def main():
    connection_string = "[Connection String]"
    signing_cert = "[Signing Cert]"
    group_id = "[Group ID]"

    #set up the provisioning service client
    psc = ProvisioningServiceClient.create_from_connection_string(
        connection_string)

    #build EnrollmentGroup model
    att = AttestationMechanism.create_with_x509_signing_certs(signing_cert)
    eg = EnrollmentGroup.create(group_id, att)

    #create EnrollmentGroup on the Provisioning Service
    eg = psc.create_or_update(eg)
    six.print_(eg)

    #get EnrollmentGroup from the Provisioning Service (note: this step is useless here, as eg is already up to date)
    eg = psc.get_enrollment_group(group_id)
    six.print_(eg)

    #make a Provisioning Service query
    qs = QuerySpecification("*")
    page_size = 2  #two results per page -> don't pass this parameter if you just want all of them at once
    query = psc.create_enrollment_group_query(qs, page_size)

    results = []
    for page in query:
        results += page
    six.print_(results)
    #alternatively, call query.next() to get a new page

    #delete EnrollmentGroup from the Provisioning Service
    psc.delete(eg)
 def test_create_enrollment_group_query_custom_page(self, mock_query):
     qs = QuerySpecification("*")
     page_size = 50
     ret = self.psc.create_enrollment_group_query(qs, page_size)
     mock_query.assert_called_with(qs, self.psc._runtime_client.device_enrollment_group.query, \
         self.psc._sastoken_factory, page_size)
     self.assertIs(ret, mock_query.return_value)
    def test_create_individual_enrollment_query_default_page(self, mock_query):

        qs = QuerySpecification("*")
        ret = self.psc.create_individual_enrollment_query(qs)
        mock_query.assert_called_with(qs, self.psc._runtime_client.device_enrollment.query, \
            self.psc._sastoken_factory, None)
        self.assertIs(ret, mock_query.return_value)
예제 #4
0
def run_scenario_individual_enrollment():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)
    att = AttestationMechanism.create_with_tpm(PROVISIONING_E2E_ENDORSEMENT_KEY)
    ie = IndividualEnrollment(registration_id=REGISTRATION_ID, attestation=att)

    #create
    ret_ie = psc.create_or_update(ie)
    assert ret_ie.registration_id == REGISTRATION_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_ie.initial_twin = twin
    capabilities = DeviceCapabilities.create(True)
    ret_ie.capabilities = capabilities

    ret_ie = psc.create_or_update(ret_ie)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES
    assert ret_ie.capabilities.iot_edge == True

    #get
    ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get attestation mechanism
    ret_am = psc.get_individual_enrollment_attestation_mechanism(REGISTRATION_ID)
    assert ret_am.tpm.endorsement_key == PROVISIONING_E2E_ENDORSEMENT_KEY

    #delete
    psc.delete(ret_ie)
    try:
        ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError

    #bulk enrollment
    enrollments = []
    for i in range(BULK_SIZE):
        new = IndividualEnrollment(registration_id=REGISTRATION_ID + str(i), attestation=att)
        enrollments.append(new)
    bulk_op = BulkEnrollmentOperation(mode=BULKOP_CREATE, enrollments=enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful

    #query
    qs = QuerySpecification(query="*")
    q = psc.create_individual_enrollment_query(qs)
    q_results = q.next()
    assert len(q_results) == BULK_SIZE

    #cleanup
    bulk_op = BulkEnrollmentOperation(mode=BULKOP_DELETE, enrollments=enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful
 def test_create_individual_enrollment_query_custom_page(self, mock_query):
     qs = QuerySpecification("*")
     page_size = 50
     ret = self.psc.create_individual_enrollment_query(qs, page_size)
     mock_query.assert_called_with(
         qs, self.psc._runtime_client.query_individual_enrollments,
         page_size)
     self.assertIs(ret, mock_query.return_value)
def main():
    connection_string = "[Connection String]"
    endorsement_key = "[Endorsement Key]"
    registration_id = "[Registration ID]"

    #set up the provisioning service client
    psc = ProvisioningServiceClient.create_from_connection_string(
        connection_string)

    #build IndividualEnrollment model
    att = AttestationMechanism.create_with_tpm(endorsement_key)
    ie = IndividualEnrollment.create(registration_id, att)

    #create IndividualEnrollment on the Provisioning Service
    ie = psc.create_or_update(ie)
    six.print_(ie)

    #get IndividualEnrollment from the Provisioning Service (note: this step is useless here, as ie is already up to date)
    ie = psc.get_individual_enrollment(registration_id)
    six.print_(ie)

    #delete IndividualEnrollment from the Provisioning Service
    psc.delete(ie)
    #could also use psc.delete_individual_enrollment_by_param(ie.registration_id, ie.etag)

    #bulk create IndividualEnrollments
    enrollments = []
    for i in range(5):
        enrollments.append(
            IndividualEnrollment.create(registration_id + str(i + 1), att))
    bulk_op = BulkEnrollmentOperation("create", enrollments)

    results = psc.run_bulk_operation(bulk_op)
    six.print_(ie)

    #make a Provisioning Service query
    qs = QuerySpecification("*")
    page_size = 2  #two results per page -> don't pass this parameter if you just want all of them at once
    query = psc.create_individual_enrollment_query(qs, page_size)

    results = []
    for page in query:
        results += page
    #alternatively, call query.next() to get a new page
    six.print_(results)

    #delete the bulk created enrollments
    bulk_op.mode = "delete"
    psc.run_bulk_operation(bulk_op)
예제 #7
0
def clear_dps_hub():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)

    #Individual Enrollments
    qs = QuerySpecification(query="*")
    query = psc.create_individual_enrollment_query(qs)
    items = []
    for page in query:
        items += page
    bulkop = BulkEnrollmentOperation(mode=BULKOP_DELETE, enrollments=items)
    psc.run_bulk_operation(bulkop)

    #Enrollment Groups
    query = psc.create_enrollment_group_query(qs)
    for page in query:
        for enrollment in page:
            psc.delete(enrollment)
예제 #8
0
def run_scenario_enrollment_group():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)
    att = AttestationMechanism.create_with_x509_signing_certs(PROVISIONING_E2E_X509_CERT)
    eg = EnrollmentGroup(enrollment_group_id=GROUP_ID, attestation=att)

    #create
    ret_eg = psc.create_or_update(eg)
    assert ret_eg.enrollment_group_id == GROUP_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_eg.initial_twin = twin
    ret_eg = psc.create_or_update(ret_eg)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get
    ret_eg = psc.get_enrollment_group(GROUP_ID)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get attestation mechansim
    ret_am = psc.get_enrollment_group_attestation_mechanism(GROUP_ID)

    #query
    qs = QuerySpecification(query="*")
    q = psc.create_enrollment_group_query(qs)
    q_results = q.next()
    assert len(q_results) == 1

    #delete
    psc.delete(ret_eg)
    try:
        ret_eg = psc.get_enrollment_group(GROUP_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError
    #create IndividualEnrollment on the Provisioning Service
    ie = psc.create_or_update(ie)
    six._print(ie)

    #get IndividualEnrollment from the Provisioning Service (note: this step is useless here, as ie is already up to date)
    ie = psc.get_individual_enrollment(registration_id)
    six._print(ie)

    #delete IndividualEnrollment from the Provisioning Service
    psc.delete(ie)
    #could also use psc.delete_individual_enrollment_by_param(ie.registration_id, ie.etag)

    #bulk create IndividualEnrollments
    enrollments = []
    for i in range(5):
        enrollments.append(IndividualEnrollment.create(registration_id + str(i + 1), att))
    bulk_op = BulkEnrollmentOperation("create", enrollments)
    
    results = psc.run_bulk_operation(bulk_op)

    #make a Provisioning Service query
    qs = QuerySpecification("*")
    page_size = 2 #two results per page -> don't pass this parameter if you just want all of them at once
    query = psc.create_individual_enrollment_query(qs, page_size)

    results = []
    for page in query:
        results += page
    #alternatively, call query.next() to get a new page
    six._print(results)
 def test_create_enrollment_group_query_default_page(self, mock_query):
     qs = QuerySpecification("*")
     ret = self.psc.create_enrollment_group_query(qs)
     mock_query.assert_called_with(
         qs, self.psc._runtime_client.query_enrollment_groups, None)
     self.assertIs(ret, mock_query.return_value)