Skip to content

Commit

Permalink
Merge pull request #209 from perfectsense/feature/pubsub
Browse files Browse the repository at this point in the history
Implement Pubsub resources
  • Loading branch information
deepanjan90 authored Feb 18, 2021
2 parents b216ee3 + c4b666f commit fcddffc
Show file tree
Hide file tree
Showing 21 changed files with 2,070 additions and 4 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ configurations {
}

dependencies {
api 'gyro:gyro-core:0.99.6' + (releaseBuild ? '' : '-SNAPSHOT')
api 'gyro:gyro-core:1.0.0' + (releaseBuild ? '' : '-SNAPSHOT')

implementation enforcedPlatform('com.google.cloud:libraries-bom:8.1.0')
implementation enforcedPlatform('com.google.cloud:libraries-bom:16.2.0')
// XXX: com.google.apis:google-api-services-iam:v1-rev316-1.25.0 uses older version of com.google.api-client:google-api-client
implementation 'com.google.api-client:google-api-client:1.30.9'
implementation 'com.google.apis:google-api-services-iam:v1-rev316-1.25.0'
Expand All @@ -73,9 +73,10 @@ dependencies {
implementation 'com.google.cloud:google-cloud-kms'
implementation 'com.google.cloud:google-cloud-resourcemanager'
implementation 'com.google.cloud:google-cloud-storage'
implementation 'com.google.cloud:google-cloud-pubsub'
implementation 'com.psddev:dari-util:3.3.607-xe0f27a'

gyroDoclet "gyro:gyro-doclet:0.99.1"
gyroDoclet "gyro:gyro-doclet:1.0.0"
}

checkstyle {
Expand Down
2 changes: 1 addition & 1 deletion codegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ repositories {
}

dependencies {
api 'gyro:gyro-core:0.99.1-SNAPSHOT'
api 'gyro:gyro-core:1.0.0-SNAPSHOT'
api 'io.airlift:airline:0.8'

implementation 'com.google.guava:guava:28.1-jre'
Expand Down
63 changes: 63 additions & 0 deletions examples/pubsub/snapshot.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
google::topic topic-example-for-snapshot
name: "topic-example-for-snapshot"

labels: {
name: "topic-example-for-snapshot"
}
end

google::subscription subscription-push-example
name: "subscription-push-example-for-snapshot"
topic: $(google::topic topic-example-for-snapshot)

ack-deadline-seconds: 10
enable-message-ordering: false
filter: ""
retain-acked-messages: true

dead-letter-policy
dead-letter-topic: $(google::topic topic-example-for-snapshot)
max-delivery-attempts: 5
end

expiration-policy
ttl
seconds: 604800
nanos: 0
end
end

retry-policy
maximum-backoff
seconds: 600
nanos: 0
end

minimum-backoff
seconds: 600
nanos: 0
end
end

message-retention
seconds: 604800
nanos: 0
end

push-config
push-endpoint: "https://google.com"
end

labels: {
name: "subscription-push-example-for-snapshot"
}
end

google::snapshot example-snapshot
name: "example-snapshot"
subscription: $(google::subscription subscription-push-example)

labels: {
name: "example-snapshot"
}
end
92 changes: 92 additions & 0 deletions examples/pubsub/subscription.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
google::topic topic-example-for-subscription
name: "topic-example-for-subscription"

labels: {
name: "topic-example-for-subscription"
}
end

google::subscription subscription-pull-example
name: "subscription-pull-example"
topic: $(google::topic topic-example-for-subscription)

ack-deadline-seconds: 15
enable-message-ordering: false
filter: ""
retain-acked-messages: false

expiration-policy
ttl
seconds: 2678400
nanos: 0
end
end

message-retention
seconds: 525780
nanos: 0
end

retry-policy
maximum-backoff
seconds: 600
nanos: 0
end

minimum-backoff
seconds: 600
nanos: 0
end
end

labels: {
name: "subscription-pull-example"
}
end

google::subscription subscription-push-example
name: "subscription-push-example"
topic: $(google::topic topic-example-for-subscription)

ack-deadline-seconds: 10
enable-message-ordering: false
filter: ""
retain-acked-messages: true

dead-letter-policy
dead-letter-topic: $(google::topic topic-example-for-subscription)
max-delivery-attempts: 5
end

expiration-policy
ttl
seconds: 604800
nanos: 0
end
end

retry-policy
maximum-backoff
seconds: 600
nanos: 0
end

minimum-backoff
seconds: 600
nanos: 0
end
end

message-retention
seconds: 604800
nanos: 0
end

push-config
push-endpoint: "https://google.com"
end

labels: {
name: "subscription-push-example"
}
end
7 changes: 7 additions & 0 deletions examples/pubsub/topic.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
google::topic topic-example
name: "topic-example"

labels: {
name: "topic-example"
}
end
24 changes: 24 additions & 0 deletions src/main/java/gyro/google/GoogleCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.cloud.kms.v1.KeyManagementServiceSettings;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.cloud.pubsub.v1.SubscriptionAdminSettings;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import gyro.core.GyroException;
import gyro.core.GyroInputStream;
import gyro.core.auth.Credentials;
Expand Down Expand Up @@ -127,6 +131,26 @@ private <T> T getNonGeneralizedClient(Class<T> clientClass) {
throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));
}
} else if (clientClass.getSimpleName().equals("TopicAdminClient")) {
try {
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(getGoogleCredentials()))
.build();
return (T) TopicAdminClient.create(topicAdminSettings);
} catch (IOException ex) {
throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));
}
} else if (clientClass.getSimpleName().equals("SubscriptionAdminClient")) {
try {
SubscriptionAdminSettings subscriptionAdminSettings = SubscriptionAdminSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(getGoogleCredentials()))
.build();
return (T) SubscriptionAdminClient.create(subscriptionAdminSettings);
} catch (IOException ex) {
throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));
}
} else {
throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/gyro/google/pubsub/DeadLetterPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021, Brightspot.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.google.pubsub;

import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Range;
import gyro.core.validation.Required;
import gyro.google.Copyable;

public class DeadLetterPolicy extends Diffable implements Copyable<com.google.pubsub.v1.DeadLetterPolicy> {

private TopicResource deadLetterTopic;
private Integer maxDeliveryAttempts;

/**
* The topic to which dead letter messages should be published.
*/
@Required
@Updatable
public TopicResource getDeadLetterTopic() {
return deadLetterTopic;
}

public void setDeadLetterTopic(TopicResource deadLetterTopic) {
this.deadLetterTopic = deadLetterTopic;
}

/**
* The maximum number of delivery attempts for any message.
*/
@Required
@Updatable
@Range(min = 5, max = 100)
public Integer getMaxDeliveryAttempts() {
return maxDeliveryAttempts;
}

public void setMaxDeliveryAttempts(Integer maxDeliveryAttempts) {
this.maxDeliveryAttempts = maxDeliveryAttempts;
}

@Override
public String primaryKey() {
return "";
}

@Override
public void copyFrom(com.google.pubsub.v1.DeadLetterPolicy model) throws Exception {
setDeadLetterTopic(findById(TopicResource.class, model.getDeadLetterTopic()));
setMaxDeliveryAttempts(model.getMaxDeliveryAttempts());
}

com.google.pubsub.v1.DeadLetterPolicy toDeadLetterPolicy() {
return com.google.pubsub.v1.DeadLetterPolicy.newBuilder()
.setDeadLetterTopic(getDeadLetterTopic().getReferenceName())
.setMaxDeliveryAttempts(getMaxDeliveryAttempts())
.build();
}
}
Loading

0 comments on commit fcddffc

Please sign in to comment.