|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "time" |
| 7 | + |
| 8 | + v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1" |
| 9 | + "connectrpc.com/connect" |
| 10 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 11 | + "google.golang.org/protobuf/types/known/fieldmaskpb" |
| 12 | +) |
| 13 | + |
| 14 | +// ListWorkloadIdentity list all workload identities using Connect RPC. |
| 15 | +func (c *client) ListWorkloadIdentity(ctx context.Context, parent string, showDeleted bool) ([]*v1pb.WorkloadIdentity, error) { |
| 16 | + if c.workloadIdentityClient == nil { |
| 17 | + return nil, errors.New("workload identity service client not initialized") |
| 18 | + } |
| 19 | + |
| 20 | + res := []*v1pb.WorkloadIdentity{} |
| 21 | + pageToken := "" |
| 22 | + startTime := time.Now() |
| 23 | + |
| 24 | + for { |
| 25 | + startTimePerPage := time.Now() |
| 26 | + |
| 27 | + req := connect.NewRequest(&v1pb.ListWorkloadIdentitiesRequest{ |
| 28 | + Parent: parent, |
| 29 | + PageSize: 500, |
| 30 | + PageToken: pageToken, |
| 31 | + ShowDeleted: showDeleted, |
| 32 | + }) |
| 33 | + |
| 34 | + resp, err := c.workloadIdentityClient.ListWorkloadIdentities(ctx, req) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + res = append(res, resp.Msg.WorkloadIdentities...) |
| 40 | + |
| 41 | + tflog.Debug(ctx, "[list workload identity per page]", map[string]interface{}{ |
| 42 | + "count": len(resp.Msg.WorkloadIdentities), |
| 43 | + "ms": time.Since(startTimePerPage).Milliseconds(), |
| 44 | + }) |
| 45 | + |
| 46 | + pageToken = resp.Msg.NextPageToken |
| 47 | + if pageToken == "" { |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + tflog.Debug(ctx, "[list workload identity]", map[string]interface{}{ |
| 53 | + "total": len(res), |
| 54 | + "ms": time.Since(startTime).Milliseconds(), |
| 55 | + }) |
| 56 | + |
| 57 | + return res, nil |
| 58 | +} |
| 59 | + |
| 60 | +// CreateWorkloadIdentity creates the workload identity using Connect RPC. |
| 61 | +func (c *client) CreateWorkloadIdentity(ctx context.Context, parent, workloadIdentityID string, workloadIdentity *v1pb.WorkloadIdentity) (*v1pb.WorkloadIdentity, error) { |
| 62 | + if c.workloadIdentityClient == nil { |
| 63 | + return nil, errors.New("workload identity service client not initialized") |
| 64 | + } |
| 65 | + |
| 66 | + req := connect.NewRequest(&v1pb.CreateWorkloadIdentityRequest{ |
| 67 | + Parent: parent, |
| 68 | + WorkloadIdentityId: workloadIdentityID, |
| 69 | + WorkloadIdentity: workloadIdentity, |
| 70 | + }) |
| 71 | + |
| 72 | + resp, err := c.workloadIdentityClient.CreateWorkloadIdentity(ctx, req) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return resp.Msg, nil |
| 78 | +} |
| 79 | + |
| 80 | +// GetWorkloadIdentity gets the workload identity by name using Connect RPC. |
| 81 | +func (c *client) GetWorkloadIdentity(ctx context.Context, name string) (*v1pb.WorkloadIdentity, error) { |
| 82 | + if c.workloadIdentityClient == nil { |
| 83 | + return nil, errors.New("workload identity service client not initialized") |
| 84 | + } |
| 85 | + |
| 86 | + req := connect.NewRequest(&v1pb.GetWorkloadIdentityRequest{ |
| 87 | + Name: name, |
| 88 | + }) |
| 89 | + |
| 90 | + resp, err := c.workloadIdentityClient.GetWorkloadIdentity(ctx, req) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + return resp.Msg, nil |
| 96 | +} |
| 97 | + |
| 98 | +// UpdateWorkloadIdentity updates the workload identity using Connect RPC. |
| 99 | +func (c *client) UpdateWorkloadIdentity(ctx context.Context, patch *v1pb.WorkloadIdentity, updateMasks []string) (*v1pb.WorkloadIdentity, error) { |
| 100 | + if c.workloadIdentityClient == nil { |
| 101 | + return nil, errors.New("workload identity service client not initialized") |
| 102 | + } |
| 103 | + |
| 104 | + req := connect.NewRequest(&v1pb.UpdateWorkloadIdentityRequest{ |
| 105 | + WorkloadIdentity: patch, |
| 106 | + UpdateMask: &fieldmaskpb.FieldMask{Paths: updateMasks}, |
| 107 | + }) |
| 108 | + |
| 109 | + resp, err := c.workloadIdentityClient.UpdateWorkloadIdentity(ctx, req) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + return resp.Msg, nil |
| 115 | +} |
| 116 | + |
| 117 | +// UndeleteWorkloadIdentity undeletes the workload identity by name using Connect RPC. |
| 118 | +func (c *client) UndeleteWorkloadIdentity(ctx context.Context, name string) (*v1pb.WorkloadIdentity, error) { |
| 119 | + if c.workloadIdentityClient == nil { |
| 120 | + return nil, errors.New("workload identity service client not initialized") |
| 121 | + } |
| 122 | + |
| 123 | + req := connect.NewRequest(&v1pb.UndeleteWorkloadIdentityRequest{ |
| 124 | + Name: name, |
| 125 | + }) |
| 126 | + |
| 127 | + resp, err := c.workloadIdentityClient.UndeleteWorkloadIdentity(ctx, req) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + return resp.Msg, nil |
| 133 | +} |
| 134 | + |
| 135 | +// DeleteWorkloadIdentity deletes the workload identity. |
| 136 | +func (c *client) DeleteWorkloadIdentity(ctx context.Context, name string) error { |
| 137 | + if c.workloadIdentityClient == nil { |
| 138 | + return errors.New("workload identity service client not initialized") |
| 139 | + } |
| 140 | + |
| 141 | + req := connect.NewRequest(&v1pb.DeleteWorkloadIdentityRequest{ |
| 142 | + Name: name, |
| 143 | + }) |
| 144 | + |
| 145 | + _, err := c.workloadIdentityClient.DeleteWorkloadIdentity(ctx, req) |
| 146 | + return err |
| 147 | +} |
0 commit comments