Exemple #1
0
def _iot_pnp_model_delete(cmd, endpoint, repository, model_id, login):
    target = get_iot_pnp_connection_string(cmd,
                                           endpoint,
                                           repository,
                                           login=login)

    pnp_sdk, errors = _bind_sdk(target, SdkType.pnp_sdk)
    try:
        headers = get_sas_token(target)
        return pnp_sdk.delete_model(model_id,
                                    repository_id=target.get(
                                        'repository_id', None),
                                    api_version=PNP_API_VERSION,
                                    custom_headers=headers)
    except errors.HttpOperationError as e:
        raise CLIError(unpack_pnp_http_error(e))
Exemple #2
0
def _iot_pnp_model_create_or_update(cmd, endpoint, repository, model_def,
                                    pnpModelType, is_update, login):

    target = get_iot_pnp_connection_string(cmd,
                                           endpoint,
                                           repository,
                                           login=login)
    pnp_sdk, errors = _bind_sdk(target, SdkType.pnp_sdk)
    etag = None
    model_def = _validate_model_definition(model_def)
    model_id = model_def.get('@id')
    if not model_id:
        raise CLIError(
            'PnP Model definition requires @id! Please include @id and try again.'
        )

    if is_update:
        model_list = _iot_pnp_model_list(cmd,
                                         endpoint,
                                         repository,
                                         model_id,
                                         pnpModelType,
                                         -1,
                                         login=login)
        if model_list and model_list[0].urn_id == model_id:
            etag = model_list[0].etag
        else:
            raise CLIError(
                'No PnP Model definition found for @id "{}"'.format(model_id))

    contents = json.loads(
        json.dumps(model_def, separators=(',', ':'), indent=2))
    try:
        headers = get_sas_token(target)
        return pnp_sdk.create_or_update_model(model_id,
                                              api_version=PNP_API_VERSION,
                                              content=contents,
                                              repository_id=target.get(
                                                  'repository_id', None),
                                              if_match=etag,
                                              custom_headers=headers)
    except errors.HttpOperationError as e:
        raise CLIError(unpack_pnp_http_error(e))
Exemple #3
0
def _iot_pnp_model_publish(cmd, endpoint, repository, model_id, model_def,
                           etag, login):

    target = get_iot_pnp_connection_string(cmd,
                                           endpoint,
                                           repository,
                                           login=login)
    pnp_sdk, errors = _bind_sdk(target, SdkType.pnp_sdk)

    contents = json.loads(
        json.dumps(model_def, separators=(',', ':'), indent=2))
    try:
        headers = get_sas_token(target)
        return pnp_sdk.create_or_update_model(model_id,
                                              api_version=PNP_API_VERSION,
                                              content=contents,
                                              if_match=etag,
                                              custom_headers=headers)
    except errors.HttpOperationError as e:
        raise CLIError(unpack_pnp_http_error(e))
Exemple #4
0
def _iot_pnp_model_list(cmd, endpoint, repository, search_string, pnpModelType,
                        top, login):
    target = get_iot_pnp_connection_string(cmd,
                                           endpoint,
                                           repository,
                                           login=login)

    pnp_sdk, errors = _bind_sdk(target, SdkType.pnp_sdk)
    try:
        headers = get_sas_token(target)
        search_options = SearchOptions(search_keyword=search_string,
                                       model_filter_type=pnpModelType.value)
        if top > 0:
            search_options.page_size = top

        result = pnp_sdk.search(search_options,
                                api_version=PNP_API_VERSION,
                                repository_id=target.get(
                                    'repository_id', None),
                                custom_headers=headers)
        return result.results
    except errors.HttpOperationError as e:
        raise CLIError(unpack_pnp_http_error(e))
Exemple #5
0
def _iot_pnp_model_show(cmd, endpoint, repository, model_id, expand,
                        pnpModelType, login):
    target = get_iot_pnp_connection_string(cmd,
                                           endpoint,
                                           repository,
                                           login=login)
    pnp_sdk, errors = _bind_sdk(target, SdkType.pnp_sdk)
    try:
        headers = get_sas_token(target)
        result = pnp_sdk.get_model(model_id,
                                   api_version=PNP_API_VERSION,
                                   repository_id=target.get(
                                       'repository_id', None),
                                   custom_headers=headers,
                                   expand=expand)

        if not result or result["@type"].lower() != pnpModelType.value.lower():
            raise CLIError(
                'PnP Model definition for "{}", not found.'.format(model_id))

        return result
    except errors.HttpOperationError as e:
        raise CLIError(unpack_pnp_http_error(e))