|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + |
| 8 | + "cloud.google.com/go/pubsub" |
| 9 | + "google.golang.org/api/option" |
| 10 | + "google.golang.org/grpc" |
| 11 | + "google.golang.org/grpc/credentials/insecure" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + GCPEndpoint = "localhost:8085" |
| 16 | + GCPProjectID = "test" |
| 17 | + GCPPublishTopicName = "outpost-publish" |
| 18 | + GCPPublishSubName = "outpost-publish-sub" |
| 19 | +) |
| 20 | + |
| 21 | +func publishGCP(body map[string]interface{}) error { |
| 22 | + log.Printf("[x] Publishing GCP PubSub") |
| 23 | + |
| 24 | + ctx := context.Background() |
| 25 | + client, err := getGCPClient(ctx) |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + defer client.Close() |
| 30 | + |
| 31 | + topic := client.Topic(GCPPublishTopicName) |
| 32 | + exists, err := topic.Exists(ctx) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + if !exists { |
| 37 | + log.Printf("[!] Topic %s does not exist. Creating it first.", GCPPublishTopicName) |
| 38 | + if err := declareGCP(); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + topic = client.Topic(GCPPublishTopicName) |
| 42 | + } |
| 43 | + |
| 44 | + messageBody, err := json.Marshal(body) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + result := topic.Publish(ctx, &pubsub.Message{ |
| 50 | + Data: messageBody, |
| 51 | + Attributes: map[string]string{ |
| 52 | + "source": "outpost-publish", |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + _, err = result.Get(ctx) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + log.Printf("[x] Published message to GCP PubSub topic %s", GCPPublishTopicName) |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func declareGCP() error { |
| 66 | + log.Printf("[*] Declaring GCP Publish infra") |
| 67 | + ctx := context.Background() |
| 68 | + client, err := getGCPClient(ctx) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + defer client.Close() |
| 73 | + |
| 74 | + // Create topic if it doesn't exist |
| 75 | + topic, err := client.CreateTopic(ctx, GCPPublishTopicName) |
| 76 | + if err != nil { |
| 77 | + if err.Error() == "rpc error: code = AlreadyExists desc = Topic already exists" { |
| 78 | + log.Printf("[*] Topic %s already exists", GCPPublishTopicName) |
| 79 | + } else { |
| 80 | + return err |
| 81 | + } |
| 82 | + } else { |
| 83 | + log.Printf("[*] Topic %s created successfully", topic.ID()) |
| 84 | + } |
| 85 | + |
| 86 | + // Create subscription if it doesn't exist |
| 87 | + _, err = client.CreateSubscription(ctx, GCPPublishSubName, pubsub.SubscriptionConfig{ |
| 88 | + Topic: topic, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + if err.Error() == "rpc error: code = AlreadyExists desc = Subscription already exists" { |
| 92 | + log.Printf("[*] Subscription %s already exists", GCPPublishSubName) |
| 93 | + } else { |
| 94 | + return err |
| 95 | + } |
| 96 | + } else { |
| 97 | + log.Printf("[*] Subscription %s created successfully", GCPPublishSubName) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func getGCPClient(ctx context.Context) (*pubsub.Client, error) { |
| 104 | + client, err := pubsub.NewClient( |
| 105 | + ctx, |
| 106 | + GCPProjectID, |
| 107 | + option.WithEndpoint(GCPEndpoint), |
| 108 | + option.WithoutAuthentication(), |
| 109 | + option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 110 | + ) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + return client, nil |
| 115 | +} |
0 commit comments