|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "google.golang.org/protobuf/types/known/emptypb" |
| 9 | + |
| 10 | + pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1" |
| 11 | +) |
| 12 | + |
| 13 | +type GetMetadataResponse struct { |
| 14 | + ID string |
| 15 | + ActiveActorsCount []*MetadataActiveActorsCount |
| 16 | + RegisteredComponents []*MetadataRegisteredComponents |
| 17 | + ExtendedMetadata map[string]string |
| 18 | + Subscriptions []*MetadataSubscription |
| 19 | + HTTPEndpoints []*MetadataHTTPEndpoint |
| 20 | +} |
| 21 | + |
| 22 | +type MetadataActiveActorsCount struct { |
| 23 | + Type string |
| 24 | + Count int32 |
| 25 | +} |
| 26 | + |
| 27 | +type MetadataRegisteredComponents struct { |
| 28 | + Name string |
| 29 | + Type string |
| 30 | + Version string |
| 31 | + Capabilities []string |
| 32 | +} |
| 33 | + |
| 34 | +type MetadataSubscription struct { |
| 35 | + PubsubName string |
| 36 | + Topic string |
| 37 | + Metadata map[string]string |
| 38 | + Rules *PubsubSubscriptionRules |
| 39 | + DeadLetterTopic string |
| 40 | +} |
| 41 | + |
| 42 | +type PubsubSubscriptionRules struct { |
| 43 | + Rules []*PubsubSubscriptionRule |
| 44 | +} |
| 45 | + |
| 46 | +type PubsubSubscriptionRule struct { |
| 47 | + Match string |
| 48 | + Path string |
| 49 | +} |
| 50 | + |
| 51 | +type MetadataHTTPEndpoint struct { |
| 52 | + Name string |
| 53 | +} |
| 54 | + |
| 55 | +// GetMetadata returns the metadata of the sidecar |
| 56 | +func (c *GRPCClient) GetMetadata(ctx context.Context) (metadata *GetMetadataResponse, err error) { |
| 57 | + resp, err := c.protoClient.GetMetadata(ctx, &emptypb.Empty{}) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("error invoking service: %w", err) |
| 60 | + } |
| 61 | + if resp != nil { |
| 62 | + activeActorsCount := make([]*MetadataActiveActorsCount, len(resp.ActiveActorsCount)) |
| 63 | + for a := range resp.ActiveActorsCount { |
| 64 | + activeActorsCount[a] = &MetadataActiveActorsCount{ |
| 65 | + Type: resp.ActiveActorsCount[a].Type, |
| 66 | + Count: resp.ActiveActorsCount[a].Count, |
| 67 | + } |
| 68 | + } |
| 69 | + registeredComponents := make([]*MetadataRegisteredComponents, len(resp.RegisteredComponents)) |
| 70 | + for r := range resp.RegisteredComponents { |
| 71 | + registeredComponents[r] = &MetadataRegisteredComponents{ |
| 72 | + Name: resp.RegisteredComponents[r].Name, |
| 73 | + Type: resp.RegisteredComponents[r].Type, |
| 74 | + Version: resp.RegisteredComponents[r].Version, |
| 75 | + Capabilities: resp.RegisteredComponents[r].Capabilities, |
| 76 | + } |
| 77 | + } |
| 78 | + subscriptions := make([]*MetadataSubscription, len(resp.Subscriptions)) |
| 79 | + for s := range resp.Subscriptions { |
| 80 | + rules := &PubsubSubscriptionRules{} |
| 81 | + for r := range resp.Subscriptions[s].Rules.Rules { |
| 82 | + rules.Rules = append(rules.Rules, &PubsubSubscriptionRule{ |
| 83 | + Match: resp.Subscriptions[s].Rules.Rules[r].Match, |
| 84 | + Path: resp.Subscriptions[s].Rules.Rules[r].Path, |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + subscriptions[s] = &MetadataSubscription{ |
| 89 | + PubsubName: resp.Subscriptions[s].PubsubName, |
| 90 | + Topic: resp.Subscriptions[s].Topic, |
| 91 | + Metadata: resp.Subscriptions[s].Metadata, |
| 92 | + Rules: rules, |
| 93 | + DeadLetterTopic: resp.Subscriptions[s].DeadLetterTopic, |
| 94 | + } |
| 95 | + } |
| 96 | + httpEndpoints := make([]*MetadataHTTPEndpoint, len(resp.HttpEndpoints)) |
| 97 | + for e := range resp.HttpEndpoints { |
| 98 | + httpEndpoints[e] = &MetadataHTTPEndpoint{ |
| 99 | + Name: resp.HttpEndpoints[e].Name, |
| 100 | + } |
| 101 | + } |
| 102 | + metadata = &GetMetadataResponse{ |
| 103 | + ID: resp.Id, |
| 104 | + ActiveActorsCount: activeActorsCount, |
| 105 | + RegisteredComponents: registeredComponents, |
| 106 | + ExtendedMetadata: resp.GetExtendedMetadata(), |
| 107 | + Subscriptions: subscriptions, |
| 108 | + HTTPEndpoints: httpEndpoints, |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return metadata, nil |
| 113 | +} |
| 114 | + |
| 115 | +// SetMetadata sets a value in the extended metadata of the sidecar |
| 116 | +func (c *GRPCClient) SetMetadata(ctx context.Context, key, value string) error { |
| 117 | + if len(key) == 0 { |
| 118 | + return errors.New("a key is required") |
| 119 | + } |
| 120 | + req := &pb.SetMetadataRequest{ |
| 121 | + Key: key, |
| 122 | + Value: value, |
| 123 | + } |
| 124 | + _, err := c.protoClient.SetMetadata(ctx, req) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("error setting metadata: %w", err) |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
0 commit comments