def on_message(self, message): obj = json.loads(message) if obj["TASK"] == "CREATE": resName = obj["NAME"] famName = obj["FAM"] modName = obj["MOD"] addr = obj["ADDR"] desc = obj["DESC"] dom = obj["DOM"] if (len(resName) > 0): try: creds = QSCreds() csapi = CloudShellAPISession(creds.Host, creds.Un, creds.Pw, creds.Dom) folder = "" parent = "" csapi.CreateResource(famName, modName, resName, addr, folder, parent, desc) csapi.AddResourcesToDomain(dom, [resName]) self.write_message("CREATED/" + message) except: e = sys.exc_info()[0] self.write_message("FAILED/EXCEPTION/" + str(e)) pass else: self.write_message("FAILED/CREATENAME") elif obj["TASK"] == "ATTR": resName = obj["NAME"] if (len(resName) > 0): try: creds = QSCreds() csapi = CloudShellAPISession(creds.Host, creds.Un, creds.Pw, creds.Dom) allRaud = [] for n, v in zip(obj["ATTS"], obj["VALS"]): if (n != "XXXNAXXX"): anv = AttributeNameValue(n, v) anvs = AttributeNamesValues(anv) raud = ResourceAttributesUpdateRequest( resName, anvs) allRaud.append(raud) csapi.SetAttributesValues(allRaud) self.write_message("ATTR/" + message) except: e = sys.exc_info()[0] self.write_message("FAILED/EXCEPTION/" + str(e)) pass else: self.write_message("FAILED/ATTRNAME") elif obj["TASK"] == "SUB": rootName = obj["ROOT"] if (len(rootName) > 0): try: creds = QSCreds() csapi = CloudShellAPISession(creds.Host, creds.Un, creds.Pw, creds.Dom) for f, m, n in zip(obj["SUBFAMS"], obj["SUBMODS"], obj["SUBNAMES"]): if (len(n) > 0): csapi.CreateResource(f, m, n, n, "", rootName, "") self.write_message("SUB/" + message) except: e = sys.exc_info()[0] self.write_message("FAILED/EXCEPTION/" + str(e)) pass else: self.write_message("FAILED/ROOTNAME") else: self.write_message("FAILED/UNKNOWN")
api.WriteMessageToReservationOutput(reservationContext["id"], "3. Configuring new domain permissions") new_group_name = domain_name api.AddNewGroup(groupName=new_group_name, description="Regular Users Group for " + domain_name + " domain", groupRole="Regular") api.AddUsersToGroup(usernames=[new_username, owner_email], groupName=new_group_name) api.AddGroupsToDomain(domainName=domain_name, groupNames=[new_group_name]) # Import content api.WriteMessageToReservationOutput(reservationContext["id"], "4. Importing Content to new domain") api_root_url = 'http://{0}:{1}/Api'.format(connectivityContext["serverAddress"], connectivityContext["qualiAPIPort"]) blueprints_to_export = [blueprint.Name for blueprint in api.GetDomainDetails("Master").Topologies] blueprints_to_export_full_names = ["Master topologies\\" + blueprint for blueprint in blueprints_to_export] api.AddTopologiesToDomain(domain_name, blueprints_to_export_full_names) # TODO: Change hardcoded CP name to dynamically assumed name api.AddResourcesToDomain(domain_name, ["AWS us-east-1"]) login_result = requests.put(api_root_url + "/Auth/Login", {"token": connectivityContext["adminAuthToken"], "domain": "Master"}) master_domain_authcode = "Basic " + login_result.content[1:-1] export_result = requests.post(api_root_url + "/Package/ExportPackage", {"TopologyNames": blueprints_to_export}, headers={"Authorization": master_domain_authcode}) login_result = requests.put(api_root_url + "/Auth/Login", {"token": connectivityContext["adminAuthToken"], "domain": domain_name}) new_domain_authcode = "Basic " + login_result.content[1:-1] import_result = requests.post(api_root_url + "/Package/ImportPackage", headers={"Authorization": new_domain_authcode}, files={'QualiPackage': export_result.content}) topologies_in_new_domain = [blueprint.Name for blueprint in api.GetDomainDetails(domain_name).Topologies] api.RemoveTopologiesFromDomain(domain_name, ["Master topologies/Vanilla Operating Systems"]) if topologies_in_new_domain != blueprints_to_export: email_title = "CloudShell Trial: Failed to setup trial" email_body = "Failed to setup trail for {user} because there was an error during import of blueprints to the new domain".format(user=new_username) smtp_client.send_email(",".join([owner_email, admin_email]), email_title, email_body, False)
from cloudshell.api.cloudshell_api import CloudShellAPISession # set domain values SOURCE_DOMAIN = "QA" TARGET_DOMAIN = "SE" # start session api = CloudShellAPISession(host="localhost", username="******", password="******", domain="Global") # find resources of target model domain_details = api.GetDomainDetails(domainName=TARGET_DOMAIN) resources = domain_details.Resources resource_names = [x.Name for x in resources] blueprints = domain_details.Topologies blueprint_names = [x.Name for x in blueprints] # add resources print(f"adding {len(resources)} resources") api.AddResourcesToDomain(domainName=TARGET_DOMAIN, resourcesNames=resource_names, includeDecendants=True) print("adding topologies") api.AddTopologiesToDomain(domainName=TARGET_DOMAIN, topologyNames=blueprint_names) print("script done")