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

New Resource: azurerm_site_recovery_vmware_replicated_vm #22477

Merged
merged 17 commits into from
Jan 30, 2024
Merged
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package custompollers

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2022-10-01/replicationprotecteditems"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

var _ pollers.PollerType = &VMWareReplicatedVMPoller{}
magodo marked this conversation as resolved.
Show resolved Hide resolved

func NewVMWareReplicatedVMPoller(client *replicationprotecteditems.ReplicationProtectedItemsClient, id replicationprotecteditems.ReplicationProtectedItemId) *VMWareReplicatedVMPoller {
return &VMWareReplicatedVMPoller{
client: client,
id: id,
}
}

type VMWareReplicatedVMPoller struct {
client *replicationprotecteditems.ReplicationProtectedItemsClient
id replicationprotecteditems.ReplicationProtectedItemId
}

func (p VMWareReplicatedVMPoller) Poll(ctx context.Context) (*pollers.PollResult, error) {
resp, err := p.client.Get(ctx, p.id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", p.id, err)
}

protectionState := ""
if model := resp.Model; model != nil && model.Properties != nil && resp.Model.Properties.ProtectionState != nil {
protectionState = *model.Properties.ProtectionState
}

if strings.EqualFold(protectionState, "Protected") || strings.EqualFold(protectionState, "normal") {
return &pollers.PollResult{
Status: pollers.PollingStatusSucceeded,
PollInterval: 1 * time.Minute,
}, nil
}

// Processing
return &pollers.PollResult{
Status: pollers.PollingStatusInProgress,
PollInterval: 1 * time.Minute,
}, 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