Java 类com.amazonaws.services.iot.model.AttachPrincipalPolicyRequest 实例源码

项目:aws-iot-fuse    文件:AwsIot.java   
public int attachPolicy(String certificateArn, String policyName) {
    AttachPrincipalPolicyRequest req = new AttachPrincipalPolicyRequest().withPrincipal(certificateArn)
            .withPolicyName(policyName);

    try {
        client.attachPrincipalPolicy(req);
    } catch (Exception e) {
        return translateException(e);
    }
    return 0;
}
项目:para    文件:AWSIoTService.java   
@Override
public Thing createThing(Thing thing) {
    if (thing == null || StringUtils.isBlank(thing.getName()) || StringUtils.isBlank(thing.getAppid()) ||
            existsThing(thing)) {
        return null;
    }
    thing.setId(Utils.getNewId());
    String id = cloudIDForThing(thing);
    String appid = thing.getAppid();

    // STEP 1: Create thing
    CreateThingResult resp1 = getClient().createThing(new CreateThingRequest().withThingName(id).
            withAttributePayload(new AttributePayload().addAttributesEntry(Config._APPID, appid)));

    // STEP 2: Create certificate
    CreateKeysAndCertificateResult resp2 = getClient().createKeysAndCertificate(
            new CreateKeysAndCertificateRequest().withSetAsActive(true));

    String accountId = getAccountIdFromARN(resp1.getThingArn());
    String policyString = (String) (thing.getDeviceMetadata().containsKey("policyJSON") ?
            thing.getDeviceMetadata().get("policyJSON") : getDefaultPolicyDocument(accountId, id));

    // STEP 3: Create policy
    getClient().createPolicy(new CreatePolicyRequest().
            withPolicyDocument(policyString).withPolicyName(id + "-Policy"));

    // STEP 4: Attach policy to certificate
    getClient().attachPrincipalPolicy(new AttachPrincipalPolicyRequest().
            withPrincipal(resp2.getCertificateArn()).withPolicyName(id + "-Policy"));

    // STEP 5: Attach thing to certificate
    getClient().attachThingPrincipal(new AttachThingPrincipalRequest().
            withPrincipal(resp2.getCertificateArn()).withThingName(id));

    thing.getDeviceMetadata().remove("policyJSON");

    thing.setServiceBroker("AWS");
    thing.getDeviceMetadata().put("thingId", thing.getId());
    thing.getDeviceMetadata().put("thingName", id);
    thing.getDeviceMetadata().put("thingARN", resp1.getThingArn());
    thing.getDeviceMetadata().put("clientId", id);
    thing.getDeviceMetadata().put("clientCertId", resp2.getCertificateId());
    thing.getDeviceMetadata().put("clientCertARN", resp2.getCertificateArn());
    thing.getDeviceMetadata().put("clientCert", resp2.getCertificatePem());
    thing.getDeviceMetadata().put("privateKey", resp2.getKeyPair().getPrivateKey());
    thing.getDeviceMetadata().put("publicKey", resp2.getKeyPair().getPublicKey());
    thing.getDeviceMetadata().put("region", Config.AWS_REGION);
    thing.getDeviceMetadata().put("port", 8883);
    thing.getDeviceMetadata().put("host", getClient().
            describeEndpoint(new DescribeEndpointRequest()).getEndpointAddress());

    return thing;
}