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

lighthouse: refactoring to use hashicorp/go-azure-sdk #17590

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
github.com/hashicorp/go-azure-helpers v0.35.0
github.com/hashicorp/go-azure-sdk v0.20220711.1122120
github.com/hashicorp/go-azure-sdk v0.20220711.1181406
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-azure-helpers v0.12.0/go.mod h1:Zc3v4DNeX6PDdy7NljlYpnrdac1++qNW0I4U+ofGwpg=
github.com/hashicorp/go-azure-helpers v0.35.0 h1:/Jpm37dzTmSHobt9SuC8bK6/jSoWw5FdxB9WIqQFXbI=
github.com/hashicorp/go-azure-helpers v0.35.0/go.mod h1:gcutZ/Hf/O7YN9M3UIvyZ9l0Rxv7Yrc9x5sSfM9cuSw=
github.com/hashicorp/go-azure-sdk v0.20220711.1122120 h1:rMNzN9nnzamkVq/YWDJ3FiGgJn05ycAY83a5+RzIUZ4=
github.com/hashicorp/go-azure-sdk v0.20220711.1122120/go.mod h1:yjQPw8DCOtQR8E8+FNaTxF6yz+tyQGkDNiVAGCNoLOo=
github.com/hashicorp/go-azure-sdk v0.20220711.1181406 h1:RxJ4xa1qkddJ/5VfQ6icUKsn+NbeAYLED85JtCySWOo=
github.com/hashicorp/go-azure-sdk v0.20220711.1181406/go.mod h1:yjQPw8DCOtQR8E8+FNaTxF6yz+tyQGkDNiVAGCNoLOo=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down
19 changes: 10 additions & 9 deletions internal/services/lighthouse/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/managedservices/mgmt/2019-06-01/managedservices"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedservices/2019-06-01/registrationassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedservices/2019-06-01/registrationdefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DefinitionsClient *managedservices.RegistrationDefinitionsClient
AssignmentsClient *managedservices.RegistrationAssignmentsClient
AssignmentsClient *registrationassignments.RegistrationAssignmentsClient
DefinitionsClient *registrationdefinitions.RegistrationDefinitionsClient
}

func NewClient(o *common.ClientOptions) *Client {
DefinitionsClient := managedservices.NewRegistrationDefinitionsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DefinitionsClient.Client, o.ResourceManagerAuthorizer)
assignmentsClient := registrationassignments.NewRegistrationAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&assignmentsClient.Client, o.ResourceManagerAuthorizer)

AssignmentsClient := managedservices.NewRegistrationAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&AssignmentsClient.Client, o.ResourceManagerAuthorizer)
definitionsClient := registrationdefinitions.NewRegistrationDefinitionsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&definitionsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
DefinitionsClient: &DefinitionsClient,
AssignmentsClient: &AssignmentsClient,
DefinitionsClient: &definitionsClient,
AssignmentsClient: &assignmentsClient,
}
}
78 changes: 40 additions & 38 deletions internal/services/lighthouse/lighthouse_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/managedservices/mgmt/2019-06-01/managedservices"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedservices/2019-06-01/registrationassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedservices/2019-06-01/registrationdefinitions"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -25,7 +25,7 @@ func resourceLighthouseAssignment() *pluginsdk.Resource {
Delete: resourceLighthouseAssignmentDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.LighthouseAssignmentID(id)
_, err := registrationassignments.ParseScopedRegistrationAssignmentID(id)
return err
}),

Expand All @@ -48,7 +48,7 @@ func resourceLighthouseAssignment() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LighthouseDefinitionID,
ValidateFunc: registrationdefinitions.ValidateScopedRegistrationDefinitionID,
},

"scope": {
Expand Down Expand Up @@ -76,27 +76,26 @@ func resourceLighthouseAssignmentCreate(d *pluginsdk.ResourceData, meta interfac
lighthouseAssignmentName = uuid
}

id := parse.NewLighthouseAssignmentID(d.Get("scope").(string), lighthouseAssignmentName)
existing, err := client.Get(ctx, id.Scope, id.Name, utils.Bool(false))
id := registrationassignments.NewScopedRegistrationAssignmentID(d.Get("scope").(string), lighthouseAssignmentName)
options := registrationassignments.GetOperationOptions{
ExpandRegistrationDefinition: utils.Bool(false),
}
existing, err := client.Get(ctx, id, options)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

parameters := managedservices.RegistrationAssignment{
Properties: &managedservices.RegistrationAssignmentProperties{
RegistrationDefinitionID: utils.String(d.Get("lighthouse_definition_id").(string)),
parameters := registrationassignments.RegistrationAssignment{
Properties: &registrationassignments.RegistrationAssignmentProperties{
RegistrationDefinitionId: d.Get("lighthouse_definition_id").(string),
},
}

future, err := client.CreateOrUpdate(ctx, id.Scope, id.Name, parameters)
if err != nil {
if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %q: %+v", id, err)
}

d.SetId(id.ID())
return resourceLighthouseAssignmentRead(d, meta)
Expand All @@ -107,14 +106,17 @@ func resourceLighthouseAssignmentRead(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LighthouseAssignmentID(d.Id())
id, err := registrationassignments.ParseScopedRegistrationAssignmentID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.Scope, id.Name, utils.Bool(false))
options := registrationassignments.GetOperationOptions{
ExpandRegistrationDefinition: utils.Bool(false),
}
resp, err := client.Get(ctx, *id, options)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[WARN] %s was not found - removing from state", *id)
d.SetId("")
return nil
Expand All @@ -123,11 +125,13 @@ func resourceLighthouseAssignmentRead(d *pluginsdk.ResourceData, meta interface{
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("name", id.RegistrationAssignmentId)
d.Set("scope", id.Scope)

if props := resp.Properties; props != nil {
d.Set("lighthouse_definition_id", props.RegistrationDefinitionID)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("lighthouse_definition_id", props.RegistrationDefinitionId)
}
}

return nil
Expand All @@ -138,45 +142,43 @@ func resourceLighthouseAssignmentDelete(d *pluginsdk.ResourceData, meta interfac
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LighthouseAssignmentID(d.Id())
id, err := registrationassignments.ParseScopedRegistrationAssignmentID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.Scope, id.Name)
if err != nil {
return fmt.Errorf("deleting Lighthouse Assignment %q at Scope %q: %+v", id.Name, id.Scope, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %q: %+v", id, err)
if err := client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Deleting"},
Target: []string{"Deleted"},
Refresh: lighthouseAssignmentDeleteRefreshFunc(ctx, client, id.Scope, id.Name),
Refresh: lighthouseAssignmentDeleteRefreshFunc(ctx, client, *id),
MinTimeout: 15 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for Lighthouse Assignment %q (Scope %q) to be deleted: %s", id.Name, id.Scope, err)
return err
}

return nil
}

func lighthouseAssignmentDeleteRefreshFunc(ctx context.Context, client *managedservices.RegistrationAssignmentsClient, scope string, lighthouseAssignmentName string) pluginsdk.StateRefreshFunc {
func lighthouseAssignmentDeleteRefreshFunc(ctx context.Context, client *registrationassignments.RegistrationAssignmentsClient, id registrationassignments.ScopedRegistrationAssignmentId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
expandLighthouseDefinition := true
res, err := client.Get(ctx, scope, lighthouseAssignmentName, &expandLighthouseDefinition)
options := registrationassignments.GetOperationOptions{
ExpandRegistrationDefinition: utils.Bool(true),
}
resp, err := client.Get(ctx, id, options)
if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, "Deleted", nil
if response.WasNotFound(resp.HttpResponse) {
return resp, "Deleted", nil
}
return nil, "Error", fmt.Errorf("issuing read request in lighthouseAssignmentDeleteRefreshFunc to Lighthouse Assignment %q (Scope %q): %s", lighthouseAssignmentName, scope, err)
return nil, "Error", fmt.Errorf("polling to check the deletion of %s: %+v", id, err)
}

return res, "Deleting", nil
return resp, "Deleting", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"testing"

"github.com/google/uuid"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedservices/2019-06-01/registrationassignments"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -90,17 +90,20 @@ func TestAccLighthouseAssignment_emptyID(t *testing.T) {
}

func (LighthouseAssignmentResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.LighthouseAssignmentID(state.ID)
id, err := registrationassignments.ParseScopedRegistrationAssignmentID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Lighthouse.AssignmentsClient.Get(ctx, id.Scope, id.Name, utils.Bool(false))
options := registrationassignments.GetOperationOptions{
ExpandRegistrationDefinition: utils.Bool(false),
}
resp, err := clients.Lighthouse.AssignmentsClient.Get(ctx, *id, options)
if err != nil {
return nil, fmt.Errorf("retrieving Lighthouse Assignment %q (Scope: %q) does not exist", id.Name, id.Scope)
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(resp.Properties != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (LighthouseAssignmentResource) basic(id string, secondTenantID string, principalID string, data acceptance.TestData) string {
Expand Down
Loading