示例#1
0
    def test_embedded_cli(self, mocked_azclient, command, subscription):
        import shlex

        cli = EmbeddedCLI()
        cli.invoke(command=command, subscription=subscription)

        # Due to forced json output
        command += " -o json"

        if subscription:
            command += " --subscription '{}'".format(subscription)

        expected_args = shlex.split(command)
        call = mocked_azclient().invoke.call_args_list[0]
        actual_args, _ = call
        assert expected_args == actual_args[0]
        success = cli.success()

        if mocked_azclient.test_meta.error_code == 1:
            assert not success
        elif mocked_azclient.test_meta.error_code == 0:
            assert success

        assert cli.output
        assert cli.as_json()
    def __init__(self, _):
        super(TestPnPModelLifecycle, self).__init__(_)
        account_settings = EmbeddedCLI().invoke(
            "account show").as_json()["user"]

        repo_id = (EmbeddedCLI().invoke(
            "iot pnp repo list --pnp-dns-suffix {}".format(
                _pnp_dns_suffix)).as_json()[0]["tenantId"])

        self.kwargs.update({
            "model": "test_model_definition.json",
            "user_id": account_settings["name"],
            "user_type": account_settings["type"],
            "repo_id": repo_id,
            "pnp_dns_suffix": _pnp_dns_suffix,
        })
    def create(self,
               name,
               resource_group_name,
               location=None,
               tags=None,
               timeout=20):
        if tags:
            tags = validate_key_value_pairs(tags)

        if not location:
            from azext_iot.common.embedded_cli import EmbeddedCLI
            resource_group_meta = EmbeddedCLI().invoke(
                "group show --name {}".format(resource_group_name)).as_json()
            location = resource_group_meta["location"]

        try:
            return self.mgmt_sdk.digital_twins.create_or_update(
                resource_name=name,
                resource_group_name=resource_group_name,
                location=location,
                tags=tags,
                long_running_operation_timeout=timeout,
            )
        except CloudError as e:
            raise e
        except ErrorResponseException as err:
            raise CLIError(unpack_msrest_error(err))
示例#4
0
    def __init__(self, test_scenario):
        assert test_scenario

        super(DTLiveScenarioTest, self).__init__(test_scenario)
        self.settings = DynamoSettings(
            opt_env_set=["azext_iot_testrg", "azext_dt_region"])
        self.embedded_cli = EmbeddedCLI()
        self._bootup_scenario()
    def __init__(self, test_scenario):
        assert test_scenario

        os.environ["AZURE_CORE_COLLECT_TELEMETRY"] = "no"
        super(DTLiveScenarioTest, self).__init__(test_scenario)
        self.settings = DynamoSettings(
            opt_env_set=["azext_iot_testrg", "azext_dt_resource_location"])
        self.embedded_cli = EmbeddedCLI()
        self._bootup_scenario()
 def setUp(self):
     if self._testMethodName == "test_repo_rbac":
         # check for TenantAdministrator
         try:
             repo_id = (EmbeddedCLI().invoke(
                 "iot pnp repo list --pnp-dns-suffix {}".format(
                     _pnp_dns_suffix)).as_json()[0]["tenantId"])
             roles = self.cmd(
                 "iot pnp role-assignment list --resource-id {0} --resource-type Tenant --subject-id {1} "
                 "--pnp-dns-suffix {2}".format(repo_id, self.user_id,
                                               _pnp_dns_suffix))
             roles = roles.get_output_in_json()
             role_assignments = list(
                 map(lambda role: role["subject"]["role"], roles))
             if RoleIdentifier.tenantAdmin.value not in role_assignments:
                 self.skipTest("Need TenantAdmin role to perform test")
         except CLIError as e:
             self.skipTest(e)
    def add_endpoint(
        self,
        name,
        endpoint_name,
        endpoint_resource_type,
        endpoint_resource_name,
        endpoint_resource_group,
        endpoint_resource_policy=None,
        endpoint_resource_namespace=None,
        endpoint_subscription=None,
        dead_letter_endpoint=None,
        tags=None,
        resource_group_name=None,
        timeout=20,
    ):
        from azext_iot.common.embedded_cli import EmbeddedCLI
        from azext_iot.digitaltwins.common import ADTEndpointType

        requires_policy = [ADTEndpointType.eventhub, ADTEndpointType.servicebus]
        if endpoint_resource_type in requires_policy:
            if not endpoint_resource_policy:
                raise CLIError(
                    "Endpoint resources of type {} require a policy name.".format(
                        " or ".join(map(str, requires_policy))
                    )
                )

            if not endpoint_resource_namespace:
                raise CLIError(
                    "Endpoint resources of type {} require a namespace.".format(
                        " or ".join(map(str, requires_policy))
                    )
                )

        target_instance = self.find_instance(
            name=name, resource_group_name=resource_group_name
        )
        if not resource_group_name:
            resource_group_name = self.get_rg(target_instance)

        cli = EmbeddedCLI()
        error_prefix = "Could not create ADT instance endpoint. Unable to retrieve"

        properties = {}

        if endpoint_resource_type == ADTEndpointType.eventgridtopic:
            eg_topic_keys_op = cli.invoke(
                "eventgrid topic key list -n {} -g {}".format(
                    endpoint_resource_name, endpoint_resource_group
                ),
                subscription=endpoint_subscription,
            )
            if not eg_topic_keys_op.success():
                raise CLIError("{} Event Grid topic keys.".format(error_prefix))
            eg_topic_keys = eg_topic_keys_op.as_json()

            eg_topic_endpoint_op = cli.invoke(
                "eventgrid topic show -n {} -g {}".format(
                    endpoint_resource_name, endpoint_resource_group
                ),
                subscription=endpoint_subscription,
            )
            if not eg_topic_endpoint_op.success():
                raise CLIError("{} Event Grid topic endpoint.".format(error_prefix))
            eg_topic_endpoint = eg_topic_endpoint_op.as_json()

            properties = EventGridEndpointProperties(
                access_key1=eg_topic_keys["key1"],
                access_key2=eg_topic_keys["key2"],
                dead_letter_secret=dead_letter_endpoint,
                topic_endpoint=eg_topic_endpoint["endpoint"],
            )

        elif endpoint_resource_type == ADTEndpointType.servicebus:
            sb_topic_keys_op = cli.invoke(
                "servicebus topic authorization-rule keys list -n {} "
                "--namespace-name {} -g {} --topic-name {}".format(
                    endpoint_resource_policy,
                    endpoint_resource_namespace,
                    endpoint_resource_group,
                    endpoint_resource_name,
                ),
                subscription=endpoint_subscription,
            )
            if not sb_topic_keys_op.success():
                raise CLIError("{} Service Bus topic keys.".format(error_prefix))
            sb_topic_keys = sb_topic_keys_op.as_json()

            properties = ServiceBusEndpointProperties(
                primary_connection_string=sb_topic_keys["primaryConnectionString"],
                secondary_connection_string=sb_topic_keys["secondaryConnectionString"],
                dead_letter_secret=dead_letter_endpoint,
            )

        elif endpoint_resource_type == ADTEndpointType.eventhub:
            eventhub_topic_keys_op = cli.invoke(
                "eventhubs eventhub authorization-rule keys list -n {} "
                "--namespace-name {} -g {} --eventhub-name {}".format(
                    endpoint_resource_policy,
                    endpoint_resource_namespace,
                    endpoint_resource_group,
                    endpoint_resource_name,
                ),
                subscription=endpoint_subscription,
            )
            if not eventhub_topic_keys_op.success():
                raise CLIError("{} Event Hub keys.".format(error_prefix))
            eventhub_topic_keys = eventhub_topic_keys_op.as_json()

            properties = EventHubEndpointProperties(
                connection_string_primary_key=eventhub_topic_keys[
                    "primaryConnectionString"
                ],
                connection_string_secondary_key=eventhub_topic_keys[
                    "secondaryConnectionString"
                ],
                dead_letter_secret=dead_letter_endpoint,
            )

        try:
            return self.mgmt_sdk.digital_twins_endpoint.create_or_update(
                resource_name=target_instance.name,
                resource_group_name=resource_group_name,
                endpoint_name=endpoint_name,
                properties=properties,
                long_running_operation_timeout=timeout,
            )
        except ErrorResponseException as e:
            raise CLIError(unpack_msrest_error(e))
示例#8
0
 def __init__(self):
     self.cli = EmbeddedCLI()
示例#9
0
    def create(
        self,
        name,
        resource_group_name,
        location=None,
        tags=None,
        timeout=60,
        assign_identity=None,
        scopes=None,
        role_type="Contributor",
        public_network_access=ADTPublicNetworkAccessType.enabled.value,
    ):
        if not location:
            from azext_iot.common.embedded_cli import EmbeddedCLI

            resource_group_meta = (
                EmbeddedCLI()
                .invoke("group show --name {}".format(resource_group_name))
                .as_json()
            )
            location = resource_group_meta["location"]

        try:
            if assign_identity:
                if scopes and not role_type:
                    raise CLIError(
                        "Both --scopes and --role values are required when assigning the instance identity."
                    )

            digital_twins_create = DigitalTwinsDescription(
                location=location,
                tags=tags,
                identity={"type": "SystemAssigned" if assign_identity else "None"},
                public_network_access=public_network_access,
            )
            create_or_update = self.mgmt_sdk.digital_twins.create_or_update(
                resource_name=name,
                resource_group_name=resource_group_name,
                digital_twins_create=digital_twins_create,
                long_running_operation_timeout=timeout,
            )

            def rbac_handler(lro):
                instance = lro.resource().as_dict()
                identity = instance.get("identity")
                if identity:
                    identity_type = identity.get("type")
                    principal_id = identity.get("principal_id")

                    if (
                        principal_id
                        and scopes
                        and identity_type
                        and identity_type.lower() == "systemassigned"
                    ):
                        for scope in scopes:
                            logger.info(
                                "Applying rbac assignment: Principal Id: {}, Scope: {}, Role: {}".format(
                                    principal_id, scope, role_type
                                )
                            )
                            self.rbac.assign_role_flex(
                                principal_id=principal_id,
                                scope=scope,
                                role_type=role_type,
                            )

            create_or_update.add_done_callback(rbac_handler)
            return create_or_update
        except CloudError as e:
            raise e
        except ErrorResponseException as err:
            raise CLIError(unpack_msrest_error(err))
 def __init__(self, test_case):
     account = EmbeddedCLI().invoke("account show").as_json()
     self.user_id = account["user"]["name"]
     self.user_type = account["user"]["type"]
     super(TestPNPRepo, self).__init__(test_case)
    def add_endpoint(
        self,
        name,
        endpoint_name,
        endpoint_resource_type,
        endpoint_resource_name,
        endpoint_resource_group,
        endpoint_resource_policy=None,
        endpoint_resource_namespace=None,
        tags=None,
        resource_group_name=None,
        timeout=20,
    ):
        from azext_iot.common.embedded_cli import EmbeddedCLI
        from azext_iot.digitaltwins.common import ADTEndpointType

        requires_policy = [
            ADTEndpointType.eventhub, ADTEndpointType.servicebus
        ]
        if endpoint_resource_type in requires_policy:
            if not endpoint_resource_policy:
                raise CLIError(
                    "Endpoint resources of type {} require a policy name.".
                    format(" or ".join(map(str, requires_policy))))

            if not endpoint_resource_namespace:
                raise CLIError(
                    "Endpoint resources of type {} require a namespace.".
                    format(" or ".join(map(str, requires_policy))))

        if tags:
            tags = validate_key_value_pairs(tags)

        target_instance = self.find_instance(
            name=name, resource_group_name=resource_group_name)
        if not resource_group_name:
            resource_group_name = self.get_rg(target_instance)

        payload = {"tags": tags}
        cli = EmbeddedCLI()
        error_prefix = "Could not create ADT instance endpoint. Unable to retrieve"
        if endpoint_resource_type == ADTEndpointType.eventgridtopic:
            eg_topic_keys_op = cli.invoke(
                "eventgrid topic key list -n {} -g {}".format(
                    endpoint_resource_name, endpoint_resource_group))
            if not eg_topic_keys_op.success():
                raise CLIError(
                    "{} Event Grid topic keys.".format(error_prefix))
            eg_topic_keys = eg_topic_keys_op.as_json()

            eg_topic_endpoint_op = cli.invoke(
                "eventgrid topic show -n {} -g {}".format(
                    endpoint_resource_name, endpoint_resource_group))
            if not eg_topic_endpoint_op.success():
                raise CLIError(
                    "{} Event Grid topic endpoint.".format(error_prefix))
            eg_topic_endpoint = eg_topic_endpoint_op.as_json()

            payload["endpointType"] = "EventGrid"
            payload["accessKey1"] = eg_topic_keys["key1"]
            payload["accessKey2"] = eg_topic_keys["key2"]
            payload["TopicEndpoint"] = eg_topic_endpoint["endpoint"]

        elif endpoint_resource_type == ADTEndpointType.servicebus:
            sb_topic_keys_op = cli.invoke(
                "servicebus topic authorization-rule keys list -n {} "
                "--namespace-name {} -g {} --topic-name {}".format(
                    endpoint_resource_policy,
                    endpoint_resource_namespace,
                    endpoint_resource_group,
                    endpoint_resource_name,
                ))
            if not sb_topic_keys_op.success():
                raise CLIError(
                    "{} Service Bus topic keys.".format(error_prefix))
            sb_topic_keys = sb_topic_keys_op.as_json()

            payload["endpointType"] = "ServiceBus"
            payload["primaryConnectionString"] = sb_topic_keys[
                "primaryConnectionString"]
            payload["secondaryConnectionString"] = sb_topic_keys[
                "secondaryConnectionString"]

        elif endpoint_resource_type == ADTEndpointType.eventhub:
            eventhub_topic_keys_op = cli.invoke(
                "eventhubs eventhub authorization-rule keys list -n {} "
                "--namespace-name {} -g {} --eventhub-name {}".format(
                    endpoint_resource_policy,
                    endpoint_resource_namespace,
                    endpoint_resource_group,
                    endpoint_resource_name,
                ))
            if not eventhub_topic_keys_op.success():
                raise CLIError("{} Event Hub keys.".format(error_prefix))
            eventhub_topic_keys = eventhub_topic_keys_op.as_json()

            payload["endpointType"] = "EventHub"
            payload["connectionString-PrimaryKey"] = eventhub_topic_keys[
                "primaryConnectionString"]
            payload["connectionString-SecondaryKey"] = eventhub_topic_keys[
                "secondaryConnectionString"]

        properties = {"properties": payload}

        try:
            return self.mgmt_sdk.digital_twins_endpoint.create_or_update(
                resource_name=target_instance.name,
                resource_group_name=resource_group_name,
                endpoint_name=endpoint_name,
                properties=properties,
                long_running_operation_timeout=timeout,
            )
        except ErrorResponseException as e:
            raise CLIError(unpack_msrest_error(e))