Skip to content

Commit

Permalink
New Resource: azurerm_site_recovery_vmware_replicated_vm (hashicorp…
Browse files Browse the repository at this point in the history
…#22477)

* New Resource: `azurerm_site_recovery_vmware_replicated_vm`

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* update test case

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* update `physical_server_credential_name`

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* refresh /vendor

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* website typo

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* golint

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* update per comments

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* add comments

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* replace `utils.String` to `pointer.To`

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* schema lint

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* update test case

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* refresh vendor

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* update code

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>

* refresh vendor

* update per comment

---------

Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>
  • Loading branch information
ziyeqf authored and rizkybiz committed Jan 30, 2024
1 parent 0eb39b2 commit 4070c8a
Show file tree
Hide file tree
Showing 46 changed files with 3,016 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package azuresdkhacks

import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
vmwaremachines "github.com/hashicorp/go-azure-sdk/resource-manager/migrate/2020-01-01/machines"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
)

// workaround for https://github.com/hashicorp/go-azure-sdk/issues/492
// TODO4.0: check if this could be removed.
// the method has been re-written to read `nextLink`

type MachinesClient struct {
Client *resourcemanager.Client
}

type Values struct {
Values *[]vmwaremachines.VMwareMachine `json:"value"`
NextLink *string `json:"nextLink"`
}

func (c MachinesClient) GetAllVMWareMachinesInSite(ctx context.Context, id vmwaremachines.VMwareSiteId, options vmwaremachines.GetAllMachinesInSiteOperationOptions) (result vmwaremachines.GetAllMachinesInSiteOperationResponse, err error) {
opts := client.RequestOptions{
ContentType: "application/json",
ExpectedStatusCodes: []int{
http.StatusOK,
},
HttpMethod: http.MethodGet,
Path: fmt.Sprintf("%s/machines", id.ID()),
OptionsObject: options,
}

req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
return
}

return wrapExecutePaged(ctx, req)
}

func wrapExecutePaged(ctx context.Context, req *client.Request) (result vmwaremachines.GetAllMachinesInSiteOperationResponse, err error) {
resp, err := req.ExecutePaged(ctx)
if resp != nil {
result.OData = resp.OData
result.HttpResponse = resp.Response
}
if err != nil {
return
}

var values Values

if err = resp.Unmarshal(&values); err != nil {
return
}
result.Model = values.Values

if values.NextLink != nil {
nextReq := req
u, err := url.Parse(*values.NextLink)
if err != nil {
return result, err
}
nextReq.URL = u
nextResp, err := wrapExecutePaged(ctx, nextReq)
if err != nil {
return result, err
}
if nextResp.Model != nil {
result.Model = pointer.To(append(*result.Model, *nextResp.Model...))
}
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package azuresdkhacks

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/go-azure-sdk/resource-manager/migrate/2020-01-01/runasaccounts"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)

// workaround for https://github.com/Azure/azure-rest-api-specs/issues/24712
// the difference is in the struct `RunAsAccountProperties`
// TODO 4.0: check if this could be removed

type RunAsAccountsClient struct {
Client *resourcemanager.Client
}

type GetAllRunAsAccountsInSiteOperationResponse struct {
HttpResponse *http.Response
OData *odata.OData
Model *[]VMwareRunAsAccount
}

type GetAllRunAsAccountsInSiteCompleteResult struct {
Items []VMwareRunAsAccount
}

// GetAllRunAsAccountsInSite ...
func (c RunAsAccountsClient) GetAllRunAsAccountsInSite(ctx context.Context, id runasaccounts.VMwareSiteId) (result GetAllRunAsAccountsInSiteOperationResponse, err error) {
opts := client.RequestOptions{
ContentType: "application/json",
ExpectedStatusCodes: []int{
http.StatusOK,
},
HttpMethod: http.MethodGet,
Path: fmt.Sprintf("%s/runAsAccounts", id.ID()),
}

req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
return
}

var resp *client.Response
resp, err = req.ExecutePaged(ctx)
if resp != nil {
result.OData = resp.OData
result.HttpResponse = resp.Response
}
if err != nil {
return
}

var values struct {
Values *[]VMwareRunAsAccount `json:"value"`
}
if err = resp.Unmarshal(&values); err != nil {
return
}

result.Model = values.Values

return
}

// GetAllRunAsAccountsInSiteComplete retrieves all the results into a single object
func (c RunAsAccountsClient) GetAllRunAsAccountsInSiteComplete(ctx context.Context, id runasaccounts.VMwareSiteId) (GetAllRunAsAccountsInSiteCompleteResult, error) {
items := make([]VMwareRunAsAccount, 0)

resp, err := c.GetAllRunAsAccountsInSite(ctx, id)
if err != nil {
err = fmt.Errorf("loading results: %+v", err)
return GetAllRunAsAccountsInSiteCompleteResult{}, err
}
if resp.Model != nil {
items = append(items, *resp.Model...)
}

return GetAllRunAsAccountsInSiteCompleteResult{Items: items}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package azuresdkhacks

import "github.com/hashicorp/go-azure-sdk/resource-manager/migrate/2020-01-01/runasaccounts"

type VMwareRunAsAccount struct {
Id *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Properties *RunAsAccountProperties `json:"properties,omitempty"`
Type *string `json:"type,omitempty"`
}

type RunAsAccountProperties struct {
CreatedTimestamp *string `json:"createdTimestamp,omitempty"`
CredentialType *runasaccounts.CredentialType `json:"credentialType,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
UpdatedTimestamp *string `json:"updatedTimestamp,omitempty"`
// ApplianceName was not defined in the original code. This is the only change.
ApplianceName *string `json:"applianceName,omitempty"`
}
18 changes: 18 additions & 0 deletions internal/services/recoveryservices/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2021-12-01/backup" // nolint: staticcheck
vmwaremachines "github.com/hashicorp/go-azure-sdk/resource-manager/migrate/2020-01-01/machines"
vmwarerunasaccounts "github.com/hashicorp/go-azure-sdk/resource-manager/migrate/2020-01-01/runasaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservices/2022-10-01/vaults"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2023-02-01/backupprotectableitems"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2023-02-01/backupprotecteditems"
Expand Down Expand Up @@ -56,6 +58,8 @@ type Client struct {
ReplicationRecoveryPlansClient *replicationrecoveryplans.ReplicationRecoveryPlansClient
ReplicationNetworksClient *replicationnetworks.ReplicationNetworksClient
ResourceGuardProxyClient *resourceguardproxy.ResourceGuardProxyClient
VMWareMachinesClient *vmwaremachines.MachinesClient
VMWareRunAsAccountsClient *vmwarerunasaccounts.RunAsAccountsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand Down Expand Up @@ -158,6 +162,18 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
resourceGuardProxyClient := resourceguardproxy.NewResourceGuardProxyClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&resourceGuardProxyClient.Client, o.ResourceManagerAuthorizer)

vmwareMachinesClient, err := vmwaremachines.NewMachinesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building VMWare Machine client: %+v", err)
}
o.Configure(vmwareMachinesClient.Client, o.Authorizers.ResourceManager)

vmwareRunAsAccountsClient, err := vmwarerunasaccounts.NewRunAsAccountsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building VMWare Run As Accounts client: %+v", err)
}
o.Configure(vmwareRunAsAccountsClient.Client, o.Authorizers.ResourceManager)

return &Client{
ProtectableItemsClient: &protectableItemsClient,
ProtectedItemsClient: &protectedItemsClient,
Expand All @@ -182,5 +198,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
ReplicationRecoveryPlansClient: replicationRecoveryPlanClient,
ReplicationNetworksClient: replicationNetworksClient,
ResourceGuardProxyClient: &resourceGuardProxyClient,
VMWareMachinesClient: vmwareMachinesClient,
VMWareRunAsAccountsClient: vmwareRunAsAccountsClient,
}, nil
}
1 change: 1 addition & 0 deletions internal/services/recoveryservices/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (r Registration) Resources() []sdk.Resource {
VMWareReplicationPolicyResource{},
VMWareReplicationPolicyAssociationResource{},
VaultGuardProxyResource{},
VMWareReplicatedVmResource{},
}
}

Expand Down
Loading

0 comments on commit 4070c8a

Please sign in to comment.