Example #1
0
 def handle(self, *args, **options):
     from oci.identity import IdentityClient
     config = oci.config.from_file()
     identity = IdentityClient(config)
     from oci.identity.models import CreateGroupDetails
     request = CreateGroupDetails()
     compartment_id = config["tenancy"]
     request.compartment_id = compartment_id
     request.name = "my-test-group"
     request.description = "Created with the Python SDK"
     group = identity.create_group(request)
     from oci.identity.models import CreateUserDetails
     request = CreateUserDetails()
     request.compartment_id = compartment_id
     request.name = "my-test-user"
     request.description = "Created with the Python SDK"
     user = identity.create_user(request)
     from oci.identity.models import AddUserToGroupDetails
     request = AddUserToGroupDetails()
     request.group_id = group.data.id
     request.user_id = user.data.id
     response = identity.add_user_to_group(request)
     print(response.status)
newCompartmentNameParts = newCompartmentName.split('-')
newCompartmentParent = compartments[newCompartmentNameParts[1].lower()]

logging.info("Creating compartment under parent '%s'" %
             newCompartmentParent.name)
newCompartment = identity.create_compartment(
    oci.identity.models.CreateCompartmentDetails(
        compartment_id=newCompartmentParent.id,
        name=newCompartmentName,
        description=newCompartmentName)).data
logging.info("New compartment: %s" % newCompartment)

logging.info("Creating compartment Admins group")
newCompartmentGroup = identity.create_group(
    oci.identity.models.CreateGroupDetails(
        compartment_id=tenant_compartment_id,
        name=newCompartmentName + "-Admins",
        description=newCompartmentName + "-Admins")).data

pks12_cert = crypto.load_pkcs12(
    automationassets.get_automation_certificate("AzureRunAsCertificate"))
pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,
                                  pks12_cert.get_privatekey())
thumbprint = automationConnection["CertificateThumbprint"]
context = adal.AuthenticationContext(
    AZURE_PUBLIC_CLOUD.endpoints.active_directory + '/' + AZTenantId)
credentials = AdalAuthentication(
    lambda: context.acquire_token_with_client_certificate(
        "https://graph.windows.net", automationConnection["ApplicationId"],
        pem_pkey, thumbprint))