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

dependencies: compute/market_place_agreements - swap to use hashicorp/go-azure-sdk #21938

Merged
merged 8 commits into from
Jun 13, 2023
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
4 changes: 3 additions & 1 deletion internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.Communication, err = communication.NewClient(o); err != nil {
return fmt.Errorf("building clients for Communication: %+v", err)
}
client.Compute = compute.NewClient(o)
if client.Compute, err = compute.NewClient(o); err != nil {
return fmt.Errorf("building clients for Compute: %+v", err)
}
if client.ConfidentialLedger, err = confidentialledger.NewClient(o); err != nil {
return fmt.Errorf("building clients for ConfidentialLedger: %+v", err)
}
Expand Down
19 changes: 12 additions & 7 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/marketplaceordering/mgmt/2015-06-01/marketplaceordering" // nolint: staticcheck
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-07-01/skus"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhostgroups"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleries"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplications"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplicationversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2021-01-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
)
Expand All @@ -38,7 +40,7 @@ type Client struct {
GalleryImagesClient *compute.GalleryImagesClient
GalleryImageVersionsClient *compute.GalleryImageVersionsClient
ImagesClient *images.ImagesClient
MarketplaceAgreementsClient *marketplaceordering.MarketplaceAgreementsClient
MarketplaceAgreementsClient *agreements.AgreementsClient
ProximityPlacementGroupsClient *proximityplacementgroups.ProximityPlacementGroupsClient
SkusClient *skus.SkusClient
SSHPublicKeysClient *sshpublickeys.SshPublicKeysClient
Expand All @@ -54,7 +56,7 @@ type Client struct {
VMImageClient *compute.VirtualMachineImagesClient
}

func NewClient(o *common.ClientOptions) *Client {
func NewClient(o *common.ClientOptions) (*Client, error) {
availabilitySetsClient := availabilitysets.NewAvailabilitySetsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&availabilitySetsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -97,8 +99,11 @@ func NewClient(o *common.ClientOptions) *Client {
imagesClient := images.NewImagesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&imagesClient.Client, o.ResourceManagerAuthorizer)

marketplaceAgreementsClient := marketplaceordering.NewMarketplaceAgreementsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&marketplaceAgreementsClient.Client, o.ResourceManagerAuthorizer)
marketplaceAgreementsClient, err := agreements.NewAgreementsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building MarketplaceAgreementsClient client: %+v", err)
}
o.Configure(marketplaceAgreementsClient.Client, o.Authorizers.ResourceManager)

proximityPlacementGroupsClient := proximityplacementgroups.NewProximityPlacementGroupsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&proximityPlacementGroupsClient.Client, o.ResourceManagerAuthorizer)
Expand Down Expand Up @@ -157,7 +162,7 @@ func NewClient(o *common.ClientOptions) *Client {
GalleryImagesClient: &galleryImagesClient,
GalleryImageVersionsClient: &galleryImageVersionsClient,
ImagesClient: &imagesClient,
MarketplaceAgreementsClient: &marketplaceAgreementsClient,
MarketplaceAgreementsClient: marketplaceAgreementsClient,
ProximityPlacementGroupsClient: &proximityPlacementGroupsClient,
SkusClient: &skusClient,
SSHPublicKeysClient: &sshPublicKeysClient,
Expand All @@ -173,5 +178,5 @@ func NewClient(o *common.ClientOptions) *Client {

// NOTE: use `VirtualMachinesClient` instead
VMClient: &vmClient,
}
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2021-01-01/agreements"
"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/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func TestAccLinuxVirtualMachine_imageFromImage(t *testing.T) {
Expand Down Expand Up @@ -412,21 +413,28 @@ provider "azurerm" {
func (r LinuxVirtualMachineResource) cancelExistingAgreement(publisher string, offer string, sku string) acceptance.ClientCheckFunc {
return func(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) error {
client := clients.Compute.MarketplaceAgreementsClient
id := parse.NewPlanID(client.SubscriptionID, publisher, offer, sku)
subscriptionId := clients.Account.SubscriptionId
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(15*time.Minute))
defer cancel()

existing, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
idGet := agreements.NewOfferPlanID(subscriptionId, publisher, offer, sku)
idCancel := agreements.NewPlanID(subscriptionId, publisher, offer, sku)

existing, err := client.MarketplaceAgreementsGet(ctx, idGet)
if err != nil {
return err
}

if props := existing.AgreementProperties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.Cancel(ctx, id.AgreementName, id.OfferName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("marketplace agreement %q does not exist", id)
if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.MarketplaceAgreementsCancel(ctx, idCancel)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("marketplace agreement %q does not exist", idGet)
}
return fmt.Errorf("canceling %s: %+v", idGet, err)
}
return fmt.Errorf("canceling Marketplace Agreement : %+v", err)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2021-01-01/agreements"
"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/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func TestAccLinuxVirtualMachineScaleSet_imagesAutomaticUpdate(t *testing.T) {
Expand Down Expand Up @@ -763,21 +764,28 @@ provider "azurerm" {
func (r LinuxVirtualMachineScaleSetResource) cancelExistingAgreement(publisher string, offer string, sku string) acceptance.ClientCheckFunc {
return func(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) error {
client := clients.Compute.MarketplaceAgreementsClient
id := parse.NewPlanID(client.SubscriptionID, publisher, offer, sku)
subscriptionId := clients.Account.SubscriptionId
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(15*time.Minute))
defer cancel()

existing, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
idGet := agreements.NewOfferPlanID(subscriptionId, publisher, offer, sku)
idCancel := agreements.NewPlanID(subscriptionId, publisher, offer, sku)

existing, err := client.MarketplaceAgreementsGet(ctx, idGet)
if err != nil {
return err
}

if props := existing.AgreementProperties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.Cancel(ctx, id.AgreementName, id.OfferName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("marketplace agreement %q does not exist", id)
if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.MarketplaceAgreementsCancel(ctx, idCancel)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("marketplace agreement %q does not exist", idGet)
}
return fmt.Errorf("canceling %s: %+v", idGet, err)
}
return fmt.Errorf("canceling Marketplace Agreement : %+v", err)
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions internal/services/compute/marketplace_agreement_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2021-01-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"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"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceMarketplaceAgreement() *pluginsdk.Resource {
Expand Down Expand Up @@ -59,25 +59,25 @@ func dataSourceMarketplaceAgreementRead(d *pluginsdk.ResourceData, meta interfac
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewPlanID(subscriptionId, d.Get("publisher").(string), d.Get("offer").(string), d.Get("plan").(string))
id := agreements.NewOfferPlanID(subscriptionId, d.Get("publisher").(string), d.Get("offer").(string), d.Get("plan").(string))

log.Printf("[DEBUG] retrieving %s", id)

term, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
term, err := client.MarketplaceAgreementsGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(term.Response) {
if !response.WasNotFound(term.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %s", id, err)
}

d.SetId(id.ID())

if props := term.AgreementProperties; props != nil {
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
if model := term.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
}
}

return nil
}
76 changes: 43 additions & 33 deletions internal/services/compute/marketplace_agreement_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2021-01-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"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 @@ -20,7 +21,7 @@ func resourceMarketplaceAgreement() *pluginsdk.Resource {
Read: resourceMarketplaceAgreementRead,
Delete: resourceMarketplaceAgreementDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.PlanID(id)
_, err := agreements.ParseOfferPlanID(id)
return err
}),

Expand Down Expand Up @@ -72,46 +73,53 @@ func resourceMarketplaceAgreementCreateUpdate(d *pluginsdk.ResourceData, meta in
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

id := parse.NewPlanID(subscriptionId, d.Get("publisher").(string), d.Get("offer").(string), d.Get("plan").(string))
id := agreements.NewOfferPlanID(subscriptionId, d.Get("publisher").(string), d.Get("offer").(string), d.Get("plan").(string))

log.Printf("[DEBUG] retrieving %s", id)

term, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
term, err := client.MarketplaceAgreementsGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(term.Response) {
if !response.WasNotFound(term.HttpResponse) {
return fmt.Errorf("retrieving %s: %s", id, err)
}
}

accepted := false
if props := term.AgreementProperties; props != nil {
if acc := props.Accepted; acc != nil {
accepted = *acc
if model := term.Model; model != nil {
if props := model.Properties; props != nil {
if acc := props.Accepted; acc != nil {
accepted = *acc
}
}
}

if accepted {
agreement, err := client.GetAgreement(ctx, id.AgreementName, id.OfferName, id.Name)
agreement, err := client.MarketplaceAgreementsGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(agreement.Response) {
if !response.WasNotFound(agreement.HttpResponse) {
return fmt.Errorf("retrieving %s: %s", id, err)
}
}
return tf.ImportAsExistsError("azurerm_marketplace_agreement", id.ID())
}

terms, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
resp, err := client.MarketplaceAgreementsGet(ctx, id)
if err != nil {
return fmt.Errorf("retrieving %s: %s", id, err)
}
if terms.AgreementProperties == nil {

if resp.Model == nil {
return fmt.Errorf("retrieving %s: Model was nil", id)
}

terms := resp.Model
if terms.Properties == nil {
return fmt.Errorf("retrieving %s: AgreementProperties was nil", id)
}

terms.AgreementProperties.Accepted = utils.Bool(true)
terms.Properties.Accepted = utils.Bool(true)

log.Printf("[DEBUG] Accepting the Marketplace Terms for %s", id)
if _, err := client.Create(ctx, id.AgreementName, id.OfferName, id.Name, terms); err != nil {
if _, err := client.MarketplaceAgreementsCreate(ctx, id, *terms); err != nil {
return fmt.Errorf("accepting Terms for %s: %s", id, err)
}
log.Printf("[DEBUG] Accepted the Marketplace Terms for %s", id)
Expand All @@ -126,35 +134,36 @@ func resourceMarketplaceAgreementRead(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.PlanID(d.Id())
id, err := agreements.ParseOfferPlanID(d.Id())
if err != nil {
return err
}

term, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
term, err := client.MarketplaceAgreementsGet(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(term.Response) {
log.Printf("[DEBUG] The Marketplace Terms was not found for Publisher %q / Offer %q / Plan %q", id.AgreementName, id.OfferName, id.Name)
if response.WasNotFound(term.HttpResponse) {
log.Printf("[DEBUG] The Marketplace Terms was not found for %s", id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving the Marketplace Terms for Publisher %q / Offer %q / Plan %q: %s", id.AgreementName, id.OfferName, id.Name, err)
return fmt.Errorf("retrieving the Marketplace Terms for %s: %s", id, err)
}

d.Set("publisher", id.AgreementName)
d.Set("offer", id.OfferName)
d.Set("plan", id.Name)
d.Set("publisher", id.PublisherId)
d.Set("offer", id.OfferId)
d.Set("plan", id.PlanId)

if props := term.AgreementProperties; props != nil {
if accepted := props.Accepted != nil && *props.Accepted; !accepted {
// if props.Accepted is not true, the agreement does not exist
d.SetId("")
if model := term.Model; model != nil {
if props := model.Properties; props != nil {
if accepted := props.Accepted != nil && *props.Accepted; !accepted {
// if props.Accepted is not true, the agreement does not exist
d.SetId("")
}
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
}
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
}

return nil
}

Expand All @@ -163,13 +172,14 @@ func resourceMarketplaceAgreementDelete(d *pluginsdk.ResourceData, meta interfac
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.PlanID(d.Id())
id, err := agreements.ParseOfferPlanID(d.Id())
if err != nil {
return err
}
idCancel := agreements.NewPlanID(id.SubscriptionId, id.PublisherId, id.OfferId, id.PlanId)

if _, err := client.Cancel(ctx, id.AgreementName, id.OfferName, id.Name); err != nil {
return fmt.Errorf("cancelling agreement for Publisher %q / Offer %q / Plan %q: %s", id.AgreementName, id.OfferName, id.Name, err)
if _, err = client.MarketplaceAgreementsCancel(ctx, idCancel); err != nil {
return fmt.Errorf("cancelling agreement for %s: %s", *id, err)
}

return nil
Expand Down
Loading