-
Notifications
You must be signed in to change notification settings - Fork 103
Migration of credential operation functions into framework #3635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,14 @@ import ( | |
| hcpsdk20240610preview "github.com/Azure/ARO-HCP/test/sdk/resourcemanager/redhatopenshifthcp/armredhatopenshifthcp" | ||
| ) | ||
|
|
||
| const ( | ||
| ProvisioningStateSucceeded = hcpsdk20240610preview.ProvisioningStateSucceeded | ||
| ProvisioningStateFailed = hcpsdk20240610preview.ProvisioningStateFailed | ||
| ProvisioningStateCanceled = hcpsdk20240610preview.ProvisioningStateCanceled | ||
| ) | ||
|
|
||
| type ProvisioningState = hcpsdk20240610preview.ProvisioningState | ||
|
|
||
| // checkOperationResult ensures the result model returned by a runtime.Poller | ||
| // matches the resource model returned from a GET request. | ||
| func checkOperationResult(expectModel, resultModel any) error { | ||
|
|
@@ -70,41 +78,18 @@ func (tc *perItOrDescribeTestContext) GetAdminRESTConfigForHCPCluster( | |
| hcpClusterName string, | ||
| timeout time.Duration, // this is a POST request, so keep the timeout as it's async | ||
| ) (*rest.Config, error) { | ||
| ctx, cancel := context.WithTimeoutCause(ctx, timeout, fmt.Errorf("timeout '%f' minutes exceeded during GetAdminRESTConfigForHCPCluster for cluster %s in resource group %s", timeout.Minutes(), hcpClusterName, resourceGroupName)) | ||
| defer cancel() | ||
|
|
||
| startTime := time.Now() | ||
| defer func() { | ||
| finishTime := time.Now() | ||
| tc.RecordTestStep("Collect admin credentials for cluster", startTime, finishTime) | ||
| }() | ||
|
|
||
| adminCredentialRequestPoller, err := hcpClient.BeginRequestAdminCredential( | ||
| ctx, | ||
| resourceGroupName, | ||
| hcpClusterName, | ||
| nil, | ||
| ) | ||
| credentialResponse, err := RequestAdminCredentialAndWait(ctx, hcpClient, resourceGroupName, hcpClusterName, timeout) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to start credential request: %w", err) | ||
| } | ||
|
|
||
| operationResult, err := adminCredentialRequestPoller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{ | ||
| Frequency: StandardPollInterval, | ||
| }) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return nil, fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish getting creds, caused by: %w, error: %w", hcpClusterName, resourceGroupName, context.Cause(ctx), err) | ||
| } | ||
| return nil, fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish getting creds: %w", hcpClusterName, resourceGroupName, err) | ||
| return nil, err | ||
| } | ||
|
|
||
| switch m := any(operationResult).(type) { | ||
| case hcpsdk20240610preview.HcpOpenShiftClustersClientRequestAdminCredentialResponse: | ||
| return readStaticRESTConfig(m.Kubeconfig) | ||
| default: | ||
| return nil, fmt.Errorf("unknown type %T", m) | ||
| } | ||
| return readStaticRESTConfig(credentialResponse.Kubeconfig) | ||
| } | ||
|
|
||
| func readStaticRESTConfig(kubeconfigContent *string) (*rest.Config, error) { | ||
|
|
@@ -128,6 +113,90 @@ func readStaticRESTConfig(kubeconfigContent *string) (*rest.Config, error) { | |
| return ret, nil | ||
| } | ||
|
|
||
| func RequestAdminCredential( | ||
| ctx context.Context, | ||
| hcpClient *hcpsdk20240610preview.HcpOpenShiftClustersClient, | ||
| resourceGroupName string, | ||
| hcpClusterName string, | ||
| ) (*runtime.Poller[hcpsdk20240610preview.HcpOpenShiftClustersClientRequestAdminCredentialResponse], error) { | ||
| return hcpClient.BeginRequestAdminCredential(ctx, resourceGroupName, hcpClusterName, nil) | ||
|
||
| } | ||
|
|
||
| func RevokeCredentials( | ||
| ctx context.Context, | ||
| hcpClient *hcpsdk20240610preview.HcpOpenShiftClustersClient, | ||
| resourceGroupName string, | ||
| hcpClusterName string, | ||
| ) (*runtime.Poller[hcpsdk20240610preview.HcpOpenShiftClustersClientRevokeCredentialsResponse], error) { | ||
| return hcpClient.BeginRevokeCredentials(ctx, resourceGroupName, hcpClusterName, nil) | ||
|
||
| } | ||
|
|
||
| func RequestAdminCredentialAndWait( | ||
| ctx context.Context, | ||
| hcpClient *hcpsdk20240610preview.HcpOpenShiftClustersClient, | ||
| resourceGroupName string, | ||
| hcpClusterName string, | ||
| timeout time.Duration, | ||
| ) (*hcpsdk20240610preview.HcpOpenShiftClustersClientRequestAdminCredentialResponse, error) { | ||
| ctx, cancel := context.WithTimeoutCause(ctx, timeout, fmt.Errorf("timeout '%f' minutes exceeded during RequestAdminCredentialAndWait for cluster %s in resource group %s", timeout.Minutes(), hcpClusterName, resourceGroupName)) | ||
| defer cancel() | ||
|
|
||
| poller, err := hcpClient.BeginRequestAdminCredential(ctx, resourceGroupName, hcpClusterName, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to start credential request for hcpCluster=%q in resourcegroup=%q: %w", hcpClusterName, resourceGroupName, err) | ||
| } | ||
|
|
||
| operationResult, err := poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{ | ||
| Frequency: StandardPollInterval, | ||
| }) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return nil, fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish getting creds, caused by: %w, error: %w", hcpClusterName, resourceGroupName, context.Cause(ctx), err) | ||
| } | ||
| return nil, fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish getting creds: %w", hcpClusterName, resourceGroupName, err) | ||
| } | ||
|
|
||
| switch m := any(operationResult).(type) { | ||
| case hcpsdk20240610preview.HcpOpenShiftClustersClientRequestAdminCredentialResponse: | ||
| return &m, nil | ||
| default: | ||
| return nil, fmt.Errorf("unknown type %T", m) | ||
| } | ||
| } | ||
|
|
||
| func RevokeCredentialsAndWait( | ||
| ctx context.Context, | ||
| hcpClient *hcpsdk20240610preview.HcpOpenShiftClustersClient, | ||
| resourceGroupName string, | ||
| hcpClusterName string, | ||
| timeout time.Duration, | ||
| ) error { | ||
| ctx, cancel := context.WithTimeoutCause(ctx, timeout, fmt.Errorf("timeout '%f' minutes exceeded during RevokeCredentialsAndWait for cluster %s in resource group %s", timeout.Minutes(), hcpClusterName, resourceGroupName)) | ||
| defer cancel() | ||
|
|
||
| poller, err := hcpClient.BeginRevokeCredentials(ctx, resourceGroupName, hcpClusterName, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to start credential revocation for hcpCluster=%q in resourcegroup=%q: %w", hcpClusterName, resourceGroupName, err) | ||
| } | ||
|
|
||
| operationResult, err := poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{ | ||
| Frequency: StandardPollInterval, | ||
| }) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish revoking creds, caused by: %w, error: %w", hcpClusterName, resourceGroupName, context.Cause(ctx), err) | ||
| } | ||
| return fmt.Errorf("failed waiting for hcpCluster=%q in resourcegroup=%q to finish revoking creds: %w", hcpClusterName, resourceGroupName, err) | ||
| } | ||
|
|
||
| switch m := any(operationResult).(type) { | ||
| case hcpsdk20240610preview.HcpOpenShiftClustersClientRevokeCredentialsResponse: | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unknown type %T", m) | ||
| } | ||
| } | ||
|
|
||
| // DeleteHCPCluster deletes an hcp cluster and waits for the operation to complete | ||
| func DeleteHCPCluster( | ||
| ctx context.Context, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't vanity alias a constant.