github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/discoveryconfig/discoveryconfig.go (about) 1 // Copyright 2023 Gravitational, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package discoveryconfig 16 17 import ( 18 "context" 19 20 "github.com/gravitational/trace" 21 22 discoveryconfigv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/discoveryconfig/v1" 23 "github.com/gravitational/teleport/api/types/discoveryconfig" 24 conv "github.com/gravitational/teleport/api/types/discoveryconfig/convert/v1" 25 ) 26 27 // Client is an DiscoveryConfig client that conforms to the following lib/services interfaces: 28 // - services.DiscoveryConfigs 29 type Client struct { 30 grpcClient discoveryconfigv1.DiscoveryConfigServiceClient 31 } 32 33 // NewClient creates a new Discovery Config client. 34 func NewClient(grpcClient discoveryconfigv1.DiscoveryConfigServiceClient) *Client { 35 return &Client{ 36 grpcClient: grpcClient, 37 } 38 } 39 40 // ListDiscoveryConfigs returns a paginated list of DiscoveryConfigs. 41 func (c *Client) ListDiscoveryConfigs(ctx context.Context, pageSize int, nextToken string) ([]*discoveryconfig.DiscoveryConfig, string, error) { 42 resp, err := c.grpcClient.ListDiscoveryConfigs(ctx, &discoveryconfigv1.ListDiscoveryConfigsRequest{ 43 PageSize: int32(pageSize), 44 NextToken: nextToken, 45 }) 46 if err != nil { 47 return nil, "", trace.Wrap(err) 48 } 49 50 discoveryConfigs := make([]*discoveryconfig.DiscoveryConfig, len(resp.DiscoveryConfigs)) 51 for i, discoveryConfig := range resp.DiscoveryConfigs { 52 var err error 53 discoveryConfigs[i], err = conv.FromProto(discoveryConfig) 54 if err != nil { 55 return nil, "", trace.Wrap(err) 56 } 57 } 58 59 return discoveryConfigs, resp.GetNextKey(), nil 60 } 61 62 // GetDiscoveryConfig returns the specified DiscoveryConfig resource. 63 func (c *Client) GetDiscoveryConfig(ctx context.Context, name string) (*discoveryconfig.DiscoveryConfig, error) { 64 resp, err := c.grpcClient.GetDiscoveryConfig(ctx, &discoveryconfigv1.GetDiscoveryConfigRequest{ 65 Name: name, 66 }) 67 if err != nil { 68 return nil, trace.Wrap(err) 69 } 70 71 discoveryConfig, err := conv.FromProto(resp) 72 return discoveryConfig, trace.Wrap(err) 73 } 74 75 // CreateDiscoveryConfig creates the DiscoveryConfig. 76 func (c *Client) CreateDiscoveryConfig(ctx context.Context, discoveryConfig *discoveryconfig.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) { 77 resp, err := c.grpcClient.CreateDiscoveryConfig(ctx, &discoveryconfigv1.CreateDiscoveryConfigRequest{ 78 DiscoveryConfig: conv.ToProto(discoveryConfig), 79 }) 80 if err != nil { 81 return nil, trace.Wrap(err) 82 } 83 dc, err := conv.FromProto(resp) 84 return dc, trace.Wrap(err) 85 } 86 87 // UpdateDiscoveryConfig updates the DiscoveryConfig. 88 func (c *Client) UpdateDiscoveryConfig(ctx context.Context, discoveryConfig *discoveryconfig.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) { 89 resp, err := c.grpcClient.UpdateDiscoveryConfig(ctx, &discoveryconfigv1.UpdateDiscoveryConfigRequest{ 90 DiscoveryConfig: conv.ToProto(discoveryConfig), 91 }) 92 if err != nil { 93 return nil, trace.Wrap(err) 94 } 95 dc, err := conv.FromProto(resp) 96 return dc, trace.Wrap(err) 97 } 98 99 // UpsertDiscoveryConfig creates or updates a DiscoveryConfig. 100 func (c *Client) UpsertDiscoveryConfig(ctx context.Context, discoveryConfig *discoveryconfig.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) { 101 resp, err := c.grpcClient.UpsertDiscoveryConfig(ctx, &discoveryconfigv1.UpsertDiscoveryConfigRequest{ 102 DiscoveryConfig: conv.ToProto(discoveryConfig), 103 }) 104 if err != nil { 105 return nil, trace.Wrap(err) 106 } 107 dc, err := conv.FromProto(resp) 108 return dc, trace.Wrap(err) 109 } 110 111 // DeleteDiscoveryConfig removes the specified DiscoveryConfig resource. 112 func (c *Client) DeleteDiscoveryConfig(ctx context.Context, name string) error { 113 _, err := c.grpcClient.DeleteDiscoveryConfig(ctx, &discoveryconfigv1.DeleteDiscoveryConfigRequest{ 114 Name: name, 115 }) 116 return trace.Wrap(err) 117 } 118 119 // DeleteAllDiscoveryConfigs removes all DiscoveryConfigs. 120 func (c *Client) DeleteAllDiscoveryConfigs(ctx context.Context) error { 121 _, err := c.grpcClient.DeleteAllDiscoveryConfigs(ctx, &discoveryconfigv1.DeleteAllDiscoveryConfigsRequest{}) 122 return trace.Wrap(err) 123 } 124 125 // UpdateDiscoveryConfigStatus updates the DiscoveryConfig Status field. 126 func (c *Client) UpdateDiscoveryConfigStatus(ctx context.Context, name string, status discoveryconfig.Status) (*discoveryconfig.DiscoveryConfig, error) { 127 resp, err := c.grpcClient.UpdateDiscoveryConfigStatus(ctx, &discoveryconfigv1.UpdateDiscoveryConfigStatusRequest{ 128 Name: name, 129 Status: conv.StatusToProto(status), 130 }) 131 if err != nil { 132 return nil, trace.Wrap(err) 133 } 134 dc, err := conv.FromProto(resp) 135 return dc, trace.Wrap(err) 136 }