github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/scim/scim.go (about)

     1  // Copyright 2024 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 scim
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/gravitational/trace"
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/protobuf/types/known/emptypb"
    23  
    24  	scimpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/scim/v1"
    25  )
    26  
    27  // Client wraps the underlying GRPC client with some more human-friendly tooling
    28  type Client struct {
    29  	grpcClient scimpb.SCIMServiceClient
    30  }
    31  
    32  func NewClientFromConn(cc grpc.ClientConnInterface) *Client {
    33  	return NewClient(scimpb.NewSCIMServiceClient(cc))
    34  }
    35  
    36  func NewClient(grpcClient scimpb.SCIMServiceClient) *Client {
    37  	return &Client{grpcClient: grpcClient}
    38  }
    39  
    40  // List fetches all (or a subset of all) resources resources of a given type
    41  func (c *Client) ListSCIMResources(ctx context.Context, req *scimpb.ListSCIMResourcesRequest) (*scimpb.ResourceList, error) {
    42  	resp, err := c.grpcClient.ListSCIMResources(ctx, req)
    43  	if err != nil {
    44  		return nil, trace.Wrap(err, "handling SCIM list request")
    45  	}
    46  	return resp, nil
    47  }
    48  
    49  // GetSCIMResource fetches a single SCIM resource from the server by name
    50  func (c *Client) GetSCIMResource(ctx context.Context, req *scimpb.GetSCIMResourceRequest) (*scimpb.Resource, error) {
    51  	resp, err := c.grpcClient.GetSCIMResource(ctx, req)
    52  	if err != nil {
    53  		return nil, trace.Wrap(err, "handling SCIM get request")
    54  	}
    55  	return resp, nil
    56  }
    57  
    58  // CreateSCIResource creates a new SCIM resource based on a supplied
    59  // resource description
    60  func (c *Client) CreateSCIMResource(ctx context.Context, req *scimpb.CreateSCIMResourceRequest) (*scimpb.Resource, error) {
    61  	resp, err := c.grpcClient.CreateSCIMResource(ctx, req)
    62  	if err != nil {
    63  		return nil, trace.Wrap(err, "handling SCIM create request")
    64  	}
    65  	return resp, nil
    66  }
    67  
    68  // UpdateSCIMResource handles a request to update a resource, returning a
    69  // representation of the updated resource
    70  func (c *Client) UpdateSCIMResource(ctx context.Context, req *scimpb.UpdateSCIMResourceRequest) (*scimpb.Resource, error) {
    71  	res, err := c.grpcClient.UpdateSCIMResource(ctx, req)
    72  	if err != nil {
    73  		return nil, trace.Wrap(err, "handling SCIM update request")
    74  	}
    75  	return res, nil
    76  }
    77  
    78  // DeleteSCIMResource handles a request to delete a resource.
    79  func (c *Client) DeleteSCIMResource(ctx context.Context, req *scimpb.DeleteSCIMResourceRequest) (*emptypb.Empty, error) {
    80  	res, err := c.grpcClient.DeleteSCIMResource(ctx, req)
    81  	if err != nil {
    82  		return nil, trace.Wrap(err, "handling SCIM delete request")
    83  	}
    84  	return res, nil
    85  }