private _createOrUpdate()

in provisioning/service/src/provisioningserviceclient.ts [357:412]


  private _createOrUpdate(endpointPrefix: string, enrollment: any, callback?: (err: Error, enrollmentResponse?: any, response?: any) => void): void {
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_009: [The `createOrUpdateIndividualEnrollment` method shall throw `ReferenceError` if the `IndividualEnrollment` argument is falsy.]*/
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_012: [The `createOrUpdateEnrollmentGroup` method shall throw `ReferenceError` if the `EnrollmentGroup` argument is falsy.] */
    if (!enrollment) {
      throw new ReferenceError('Required parameter enrollment was null or undefined when calling createOrUpdate.');
    }

    let id: string;
    if (endpointPrefix === this._enrollmentGroupsPrefix) {
      id = enrollment.enrollmentGroupId;
    } else {
      id = enrollment.registrationId;
    }

    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_011: [The `createOrUpdateIndividualEnrollment` method shall throw `ArgumentError` if the `enrollment.registrationId` property is falsy.] */
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_013: [`createOrUpdateEnrollmentGroup` method shall throw `ArgumentError` if the `enrollmentGroup.enrollmentGroupsId` property is falsy.] */
    if (!id) {
      throw new ArgumentError('Required id property was null or undefined when calling createOrUpdate.');
    }
    const path = endpointPrefix + encodeURIComponent(id) + this._versionQueryString();

    const httpHeaders = {
      'Accept': 'application/json',
      'Content-Type': 'application/json; charset=utf-8'
    };

    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_055: [If the `enrollmentGroup` object contains an `etag` property it will be added as the value of the `If-Match` header of the http request.] */
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_056: [If the `enrollment` object contains an `etag` property it will be added as the value of the `If-Match` header of the http request.] */
    if (enrollment.etag) {
      httpHeaders['If-Match'] = enrollment.etag;
    }
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_010: [The `createOrUpdateIndividualEnrollment` method shall construct an HTTP request using information supplied by the caller, as follows:
      PUT /enrollments/<uri-encoded-enrollment.registrationId>?api-version=<version> HTTP/1.1
      Authorization: <sharedAccessSignature>
      Accept: application/json
      Content-Type: application/json; charset=utf-8

      <stringified json string of the enrollment argument>]*/
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_014: [The `createOrUpdateEnrollmentGroup` method shall construct an HTTP request using information supplied by the caller, as follows:
      PUT /enrollmentGroups/<uri-encoded-enrollmentGroup.enrollmentGroupsId>?api-version=<version> HTTP/1.1
      Authorization: <sharedAccessSignature>
      Accept: application/json
      Content-Type: application/json; charset=utf-8

      <stringified json string of the enrollmentGroup argument>
      ] */
    this._restApiClient.executeApiCall('PUT', path, httpHeaders, enrollment, (err, enrollmentResponse, httpResponse) => {
      if (callback) {
        if (err) {
          callback(err);
        } else {
          callback(null, enrollmentResponse, httpResponse);
        }
      }
    });
  }