Skip to content

Commit

Permalink
ocm: Add break-glass credential methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Barnes committed Feb 26, 2025
1 parent 9912725 commit 9bfeec0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
58 changes: 58 additions & 0 deletions internal/mocks/ocm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions internal/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ type ClusterServiceClientSpec interface {
// the returned iterator in a for/range loop to execute the request and paginate over results,
// then call GetError() to check for an iteration error.
ListNodePools(clusterInternalID InternalID, searchExpression string) NodePoolListIterator

// GetBreakGlassCredential sends a GET request to fetch a break-glass cluster credential from Cluster Service.
GetBreakGlassCredential(ctx context.Context, internalID InternalID) (*cmv1.BreakGlassCredential, error)

// PostBreakGlassCredential sends a POST request to create a break-glass cluster credential in Cluster Service.
PostBreakGlassCredential(ctx context.Context, clusterInternalID InternalID) (*cmv1.BreakGlassCredential, error)

// DeleteBreakGlassCredentials sends a DELETE request to revoke all break-glass credentials for a cluster in Cluster Service.
DeleteBreakGlassCredentials(ctx context.Context, clusterInternalID InternalID) error

// ListBreakGlassCredentials prepares a GET request with the given search expression. Call
// Items() on the returned iterator in a for/range loop to execute the request and paginate
// over results, then call GetError() to check for an iteration error.
ListBreakGlassCredentials(clusterInternalID InternalID, searchExpression string) BreakGlassCredentialListIterator
}

type ClusterServiceClient struct {
Expand Down Expand Up @@ -230,3 +244,56 @@ func (csc *ClusterServiceClient) ListNodePools(clusterInternalID InternalID, sea
}
return NodePoolListIterator{request: nodePoolsListRequest}
}

func (csc *ClusterServiceClient) GetBreakGlassCredential(ctx context.Context, internalID InternalID) (*cmv1.BreakGlassCredential, error) {
client, ok := internalID.GetBreakGlassCredentialClient(csc.Conn)
if !ok {
return nil, fmt.Errorf("OCM path is not a break-glass credential: %s", internalID)
}
breakGlassCredentialGetResponse, err := client.Get().SendContext(ctx)
if err != nil {
return nil, err
}
breakGlassCredential, ok := breakGlassCredentialGetResponse.GetBody()
if !ok {
return nil, fmt.Errorf("empty response body")
}
return breakGlassCredential, nil
}

func (csc *ClusterServiceClient) PostBreakGlassCredential(ctx context.Context, clusterInternalID InternalID) (*cmv1.BreakGlassCredential, error) {
client, ok := clusterInternalID.GetClusterClient(csc.Conn)
if !ok {
return nil, fmt.Errorf("OCM path is not a cluster: %s", clusterInternalID)
}
breakGlassCredentialsAddResponse, err := client.BreakGlassCredentials().Add().SendContext(ctx)
if err != nil {
return nil, err
}
breakGlassCredential, ok := breakGlassCredentialsAddResponse.GetBody()
if !ok {
return nil, fmt.Errorf("empty response body")
}
return breakGlassCredential, nil
}

func (csc *ClusterServiceClient) DeleteBreakGlassCredentials(ctx context.Context, clusterInternalID InternalID) error {
client, ok := clusterInternalID.GetClusterClient(csc.Conn)
if !ok {
return fmt.Errorf("OCM path is not a cluster: %s", clusterInternalID)
}
_, err := client.BreakGlassCredentials().Delete().SendContext(ctx)
return err
}

func (csc *ClusterServiceClient) ListBreakGlassCredentials(clusterInternalID InternalID, searchExpression string) BreakGlassCredentialListIterator {
client, ok := clusterInternalID.GetClusterClient(csc.Conn)
if !ok {
return BreakGlassCredentialListIterator{err: fmt.Errorf("OCM path is not a cluster: %s", clusterInternalID)}
}
breakGlassCredentialsListRequest := client.BreakGlassCredentials().List()
if searchExpression != "" {
breakGlassCredentialsListRequest.Search(searchExpression)
}
return BreakGlassCredentialListIterator{request: breakGlassCredentialsListRequest}
}

0 comments on commit 9bfeec0

Please sign in to comment.