com.google.protobuf.util.FieldMaskUtil Java Examples

The following examples show how to use com.google.protobuf.util.FieldMaskUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void anyInMaps() throws Exception {
  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
 
Example #2
Source File: UpdateKeyRemoveRotation.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public void updateKeyRemoveRotation(
    String projectId, String locationId, String keyRingId, String keyId) throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, key ring, and keyId.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Build an empty key with no labels.
    CryptoKey key =
        CryptoKey.newBuilder()
            .setName(cryptoKeyName.toString())
            .clearRotationPeriod()
            .clearNextRotationTime()
            .build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");

    // Create the key.
    CryptoKey createdKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", createdKey.getName());
  }
}
 
Example #3
Source File: UpdateKeyRemoveLabels.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public void updateKeyRemoveLabels(
    String projectId, String locationId, String keyRingId, String keyId) throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, key ring, and keyId.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Build an empty key with no labels.
    CryptoKey key = CryptoKey.newBuilder().setName(cryptoKeyName.toString()).build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("labels");

    // Create the key.
    CryptoKey createdKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", createdKey.getName());
  }
}
 
Example #4
Source File: SnippetsIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void afterAll() throws IOException {
  Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));

  // Iterate over each key ring's key's crypto key versions and destroy.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
      if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
        CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
        FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
        client.updateCryptoKey(keyWithoutRotation, fieldMask);
      }

      ListCryptoKeyVersionsRequest listVersionsRequest =
          ListCryptoKeyVersionsRequest.newBuilder()
              .setParent(key.getName())
              .setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED")
              .build();
      for (CryptoKeyVersion version :
          client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
        client.destroyCryptoKeyVersion(version.getName());
      }
    }
  }
}
 
Example #5
Source File: UpdateSecret.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public void updateSecret(String projectId, String secretId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
    // Build the name.
    SecretName secretName = SecretName.of(projectId, secretId);

    // Build the updated secret.
    Secret secret =
        Secret.newBuilder()
            .setName(secretName.toString())
            .putLabels("secretmanager", "rocks")
            .build();

    // Build the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("labels");

    // Create the secret.
    Secret updatedSecret = client.updateSecret(secret, fieldMask);
    System.out.printf("Updated secret %s\n", updatedSecret.getName());
  }
}
 
Example #6
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void fieldMask() throws Exception {
  TestFieldMask message =
      TestFieldMask.newBuilder()
          .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz,foo_bar.baz"))
          .build();
  assertMatchesUpstream(message);
}
 
Example #7
Source File: DisableKeyVersion.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void disableKeyVersion(
    String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
    throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the key version name from the project, location, key ring, key,
    // and key version.
    CryptoKeyVersionName keyVersionName =
        CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

    // Build the updated key version, setting it to disbaled.
    CryptoKeyVersion keyVersion =
        CryptoKeyVersion.newBuilder()
            .setName(keyVersionName.toString())
            .setState(CryptoKeyVersionState.DISABLED)
            .build();

    // Create a field mask of updated values.
    FieldMask fieldMask = FieldMaskUtil.fromString("state");

    // Destroy the key version.
    CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
    System.out.printf("Disabled key version: %s%n", response.getName());
  }
}
 
Example #8
Source File: UpdateKeyUpdateLabels.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void updateKeyUpdateLabels(
    String projectId, String locationId, String keyRingId, String keyId) throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the parent name from the project, location, and key ring.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    //
    // Step 1 - get the current set of labels on the key
    //

    // Get the current key.
    CryptoKey key = client.getCryptoKey(cryptoKeyName);

    //
    // Step 2 - add a label to the list of labels
    //

    // Add a new label.
    key = key.toBuilder().putLabels("new_label", "new_value").build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("labels");

    // Update the key.
    CryptoKey updatedKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", updatedKey.getName());
  }
}
 
Example #9
Source File: EnableKeyVersion.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void enableKeyVersion(
    String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
    throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the key version name from the project, location, key ring, key,
    // and key version.
    CryptoKeyVersionName keyVersionName =
        CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

    // Build the updated key version, setting it to enabled.
    CryptoKeyVersion keyVersion =
        CryptoKeyVersion.newBuilder()
            .setName(keyVersionName.toString())
            .setState(CryptoKeyVersionState.ENABLED)
            .build();

    // Create a field mask of updated values.
    FieldMask fieldMask = FieldMaskUtil.fromString("state");

    // Destroy the key version.
    CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
    System.out.printf("Enabled key version: %s%n", response.getName());
  }
}
 
Example #10
Source File: UpdateKeyAddRotation.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void updateKeyAddRotation(
    String projectId, String locationId, String keyRingId, String keyId) throws IOException {
  // Initialize client that will be used to send requests. This client only
  // needs to be created once, and can be reused for multiple requests. After
  // completing all of your requests, call the "close" method on the client to
  // safely clean up any remaining background resources.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, and key ring.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Calculate the date 24 hours from now (this is used below).
    long tomorrow = java.time.Instant.now().plus(24, ChronoUnit.HOURS).getEpochSecond();

    // Build the key to update with a rotation schedule.
    CryptoKey key =
        CryptoKey.newBuilder()
            .setName(cryptoKeyName.toString())
            .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
            .setVersionTemplate(
                CryptoKeyVersionTemplate.newBuilder()
                    .setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION))

            // Rotate every 30 days.
            .setRotationPeriod(
                Duration.newBuilder().setSeconds(java.time.Duration.ofDays(30).getSeconds()))

            // Start the first rotation in 24 hours.
            .setNextRotationTime(Timestamp.newBuilder().setSeconds(tomorrow))
            .build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");

    // Update the key.
    CryptoKey updatedKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", updatedKey.getName());
  }
}
 
Example #11
Source File: FieldMaskDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public FieldMask deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_STRING:
      return FieldMaskUtil.fromJsonString(parser.getText());
    default:
      context.reportWrongTokenException(FieldMask.class, JsonToken.VALUE_STRING, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example #12
Source File: FieldMaskSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
        FieldMask fieldMask,
        JsonGenerator generator,
        SerializerProvider serializerProvider
) throws IOException {
  generator.writeString(FieldMaskUtil.toJsonString(fieldMask));
}
 
Example #13
Source File: WellKnownTypeMarshaller.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder)
    throws IOException {
  FieldMask.Builder builder = (FieldMask.Builder) messageBuilder;
  builder.mergeFrom(FieldMaskUtil.fromJsonString(ParseSupport.parseString(parser)));
}
 
Example #14
Source File: WellKnownTypeMarshaller.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public void doWrite(FieldMask message, JsonGenerator gen) throws IOException {
  gen.writeString(FieldMaskUtil.toJsonString(message));
}
 
Example #15
Source File: MessageMarshallerTest.java    From curiostack with MIT License 4 votes vote down vote up
@Test
public void anyFields() throws Exception {
  TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
  TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
  assertMatchesUpstream(message, TestAllTypes.getDefaultInstance());

  TestAny messageWithDefaultAnyValue =
      TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
  assertMatchesUpstream(messageWithDefaultAnyValue);

  // Well-known types have a special formatting when embedded in Any.
  //
  // 1. Any in Any.
  Any anyMessage = Any.pack(Any.pack(content));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 2. Wrappers in Any.
  anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage =
      Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 3. Timestamp in Any.
  anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 4. Duration in Any
  anyMessage = Any.pack(Durations.parse("12345.10s"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 5. FieldMask in Any
  anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 6. Struct in Any
  Struct.Builder structBuilder = Struct.newBuilder();
  structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
  anyMessage = Any.pack(structBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 7. Value (number type) in Any
  Value.Builder valueBuilder = Value.newBuilder();
  valueBuilder.setNumberValue(1);
  anyMessage = Any.pack(valueBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 8. Value (null type) in Any
  anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
}