Skip to content
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

Updating listSecrets to use the new Azure Client #4862

Merged
merged 2 commits into from
Dec 22, 2022

Conversation

ytimocin
Copy link
Contributor

@ytimocin ytimocin commented Dec 14, 2022

Description

Clients migrated:

  1. Custom Action

Functions:

  1. GetResourceLocation Function

Work in Progress:

  1. Unit Test
  2. Functional Test

Issue reference

Fixes: #issue_number

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Adds necessary unit tests for change
  • Adds necessary E2E tests for change
  • Unit tests passing
  • Extended the documentation / Created issue for it

@ytimocin ytimocin marked this pull request as ready for review December 14, 2022 22:56
@ytimocin ytimocin requested a review from a team as a code owner December 14, 2022 22:56
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch 2 times, most recently from 9f447ac to 63a4a20 Compare December 15, 2022 00:18
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from 63a4a20 to adb9534 Compare December 15, 2022 02:59
@ytimocin ytimocin force-pushed the ytimocin/azureClientV2 branch from 360d44e to e9d5d18 Compare December 15, 2022 17:12
Base automatically changed from ytimocin/azureClientV2 to main December 15, 2022 18:04
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from adb9534 to cbd7f79 Compare December 15, 2022 18:08
custom := clients.NewCustomActionClient(parsed.FindScope(resources.SubscriptionsSegment), c.ARM.Auth)
response, err := custom.InvokeCustomAction(ctx, arm.ID, arm.APIVersion, action, nil)
subscriptionID := parsed.FindScope(resources.SubscriptionsSegment)
client, err := clientv2.NewCustomActionClient(subscriptionID, c.ARM.ClientOption.Cred)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're taking a resource ID in BeginCustomAction do we need to pass the subscription id here? That would simplify life if we don't have to.

@@ -27,7 +28,7 @@ type CustomActionClient struct {
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address all missing feedbacks in #4851

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the unit tests; the tests in the current client (not v2) are as follows:

The first two only have one test in each. And those tests can't be moved over to the new client because those functions don't exist anymore in client v2.

unfold.go will be handled in another PR and therefore the tests will be also moved/changed accordingly in that PR.

I will also have another PR just for adding/polishing unit tests after we migrate to the new clients.

Please let me know if that doesn't for you @youngbupark @rynowak. I am open to suggestions of any kind.

Right now this PR is the step 1 of the migration to client v2.

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch 4 times, most recently from 882e918 to c36dad9 Compare December 15, 2022 22:21
pkg/azure/clientv2/clients.go Outdated Show resolved Hide resolved
pkg/azure/clientv2/clients.go Outdated Show resolved Hide resolved
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch 2 times, most recently from 7090307 to f081ad6 Compare December 16, 2022 22:32
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from f081ad6 to 4127268 Compare December 19, 2022 17:46
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from 4127268 to baf08ef Compare December 19, 2022 18:19
pkg/azure/clientv2/common.go Show resolved Hide resolved
pkg/azure/clientv2/customaction.go Outdated Show resolved Hide resolved
if err != nil {
return nil, err
}

pipeline, err := armruntime.NewPipeline(moduleName, moduleVersion, credential, runtime.PipelineOptions{}, options)
pipeline, err := armruntime.NewPipeline(ModuleName, ModuleVersion, options.Cred, runtime.PipelineOptions{}, armClientOptions)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it means customactionclient always requires the credentials, but client.CustomActionClient does not use any authorizer. Can you please review CustomActionClient usage again?

Also if cred is not required, then how should we use newclient without cred?

return nil, err
}

options := clientv2.NewClientBeginCustomActionOptions(arm.ID, action, arm.APIVersion)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need additional Begin* function ? I do not think we need to split InvokeCustomAction to NewClientBeginCustomActionOptions and BeginCustomAction.

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from baf08ef to 2e662b8 Compare December 20, 2022 00:09
// ClientOption is the client v2 option including new client credentials.
ClientOption clientv2.AzureClientOption
// ClientOptions is the client v2 options including new client credentials.
ClientOptions clientv2.Options
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I named this as ClientOptions is to specify that this is the Options struct that is in the Clients package.

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from 2e662b8 to c4593a7 Compare December 20, 2022 00:19
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/project-radius/radius/pkg/ucp/resources"
)

type CustomActionClient struct {
*BaseClient
type ClientCustomActionResponse struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments to all public struct/methods/members...

pkg/azure/clientv2/resourcedeploymentclient.go Outdated Show resolved Hide resolved
@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch 3 times, most recently from 108a32f to b40e4cf Compare December 20, 2022 16:56
pkg/azure/clientv2/customaction.go Outdated Show resolved Hide resolved
pkg/azure/clientv2/customaction.go Outdated Show resolved Hide resolved
pkg/azure/clientv2/customaction.go Outdated Show resolved Hide resolved
// Cred represents a credential for OAuth token.
Cred azcore.TokenCredential

BaseURI string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this BaseURI here ? please add comment to public member.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still use this as the way to pass in a BaseURI to the client. I am going to see in the other PRs if we actually need this, and then update if necessary.

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from b40e4cf to 9f76a56 Compare December 20, 2022 19:18
}

func NewClientBeginCreateOrUpdateOptions(resourceID, resumeToken, apiVersion string) *ClientBeginCreateOrUpdateOptions {
// FIXME: This is to validate the resourceID.
func NewClientBeginCreateOrUpdateOptions(resourceID, apiVersion string) *ClientBeginCreateOrUpdateOptions {
Copy link

@youngbupark youngbupark Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have the reason why you introduce Begin pattern? the older client doesn't have this begin. To me, begin* is an unnecessary helper. Please make it simple.

Copy link

@youngbupark youngbupark Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, this clientv2 pkg wraps the client, but we do not need to follow the generated client pattern. Generator has to use the same pattern to generate client even though it is inefficient. I strongly recommend to follow the older client pattern although the implementation is different.

So, we do not need ClientBeginCreateOrUpdateOptions. We only need two methods like below.

// ....

func (client *DeploymentsClient) CreateOrUpdate(ctx context.Context,  resourceID string, parameters Deployment) (*runtime.Poller[ClientCreateOrUpdateResponse], error) {
      // validate resourceID
     req, err := client.createCreateOrUpdateRequest(ctx, resourceID, parameters)
     resp, err := client.Pipeline.Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(resp, http.StatusOK, http.StatusCreated, http.StatusAccepted) {
		return nil, runtime.NewResponseError(resp)
	}
      return runtime.NewPoller[ClientCreateOrUpdateResponse](resp, *client.Pipeline, nil)
}

func (client *DeploymentsClient) createCreateOrUpdateRequest(ctx context.Context, resourceID string, parameters Deployment) (*policy.Request, error) {
// return Request
}

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from 9f76a56 to c7b6c3b Compare December 20, 2022 19:48
Copy link

@youngbupark youngbupark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment on lines 74 to 79
urlPath = strings.ReplaceAll(urlPath, "{resourceID}", url.PathEscape(resourceID))

if opts.action == "" {
if action == "" {
return nil, errors.New("action cannot be empty")
}
urlPath = strings.ReplaceAll(urlPath, "{action}", url.PathEscape(opts.action))
urlPath = strings.ReplaceAll(urlPath, "{action}", url.PathEscape(action))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: is there a reason why we use ReplaceAll?
Can we just use fmt.sprintf ?

        if resourceID == "" {
		return nil, errors.New("resourceID cannot be empty")
	}
        if action == "" {
		return nil, errors.New("action cannot be empty")
	}
        urlPath := fmt.Sprintf("%s/%s", url.PathEscape(resourceID), url.PathEscape(action))

Copy link

@youngbupark youngbupark Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can also use runtime.JoinPaths instead of urlPath.

       if resourceID == "" {
		return nil, errors.New("resourceID cannot be empty")
	}
        if action == "" {
		return nil, errors.New("action cannot be empty")
	}
        urlPath := runtime.JoinPaths(client.baseURI, url.PathEscape(resourceID), url.PathEscape(action))
        req, err := runtime.NewRequest(ctx, http.MethodPost, urlPath)

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch 2 times, most recently from 0aff153 to 8a56a94 Compare December 22, 2022 19:27
}
return resp, nil
}
urlPath := "{resourceID}/{action}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need this anymore.

@ytimocin ytimocin force-pushed the ytimocin/clientV2/customAction branch from 8a56a94 to f2631b4 Compare December 22, 2022 20:05
@ytimocin ytimocin merged commit 6bf7ff8 into main Dec 22, 2022
@ytimocin ytimocin deleted the ytimocin/clientV2/customAction branch December 22, 2022 20:37
mishrapratikshya pushed a commit that referenced this pull request Feb 2, 2023
# Description
Clients migrated:
1. Custom Action

Functions:
1. GetResourceLocation Function

Work in Progress:
1. Unit Test
3. Functional Test

## Issue reference
Fixes: #issue_number

## Checklist

Please make sure you've completed the relevant tasks for this PR, out of
the following list:

* [ ] Code compiles correctly
* [ ] Adds necessary unit tests for change
* [ ] Adds necessary E2E tests for change
* [ ] Unit tests passing
* [ ] Extended the documentation / Created issue for it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants