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: managed applications: swap to hashicorp/go-azure-sdk #21571

Merged
merged 27 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dcb86b2
managed applications: swap to go-azure-sdk
katbyte Apr 27, 2023
8692ad7
go mod tidy && go mod vendor
tombuildsstuff Apr 27, 2023
3c8c7ef
Update internal/services/managedapplications/client/client.go
katbyte May 24, 2023
25fe242
Update internal/services/managedapplications/client/client.go
katbyte May 24, 2023
cebb238
Update internal/services/managedapplications/managed_application_reso…
katbyte May 24, 2023
e9c663f
Update internal/services/managedapplications/managed_application_defi…
katbyte May 24, 2023
5cd3aaa
Update internal/services/managedapplications/managed_application_defi…
katbyte May 24, 2023
217482d
Update internal/services/managedapplications/managed_application_defi…
katbyte May 24, 2023
c77b253
Update internal/services/managedapplications/managed_application_defi…
katbyte May 24, 2023
2d29eee
Update internal/services/managedapplications/managed_application_reso…
katbyte May 24, 2023
0f8b1e4
address review comments
katbyte May 24, 2023
60d7de3
merge
katbyte May 24, 2023
14f0dbf
merge main
katbyte May 24, 2023
c8c10a1
go mod vendor; go mod tidy
katbyte May 24, 2023
3b9886a
fix build
katbyte May 24, 2023
192a15e
Merge branch 'main' into kt/managed-pandora-applications
katbyte May 24, 2023
d2392aa
fix build
katbyte May 24, 2023
812db09
Update internal/services/managedapplications/managed_application_reso…
katbyte May 24, 2023
d4ea84a
fix panic
katbyte Jun 27, 2023
e835c61
merge main
katbyte Jun 27, 2023
b4988cf
go mod vendor & tidy
katbyte Jun 27, 2023
8f1b56c
merge with main
mbfrahry Jul 7, 2023
56762cd
Fix test failures
mbfrahry Jul 7, 2023
ede54dd
add crash checks
mbfrahry Jul 7, 2023
e12f8e6
addressing review
mbfrahry Jul 7, 2023
49b7ddb
goimports
mbfrahry Jul 7, 2023
2df7669
merge with main
mbfrahry Jul 7, 2023
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 @@ -391,7 +391,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.Logz = logz.NewClient(o)
client.MachineLearning = machinelearning.NewClient(o)
client.Maintenance = maintenance.NewClient(o)
client.ManagedApplication = managedapplication.NewClient(o)
if client.ManagedApplication, err = managedapplication.NewClient(o); err != nil {
return fmt.Errorf("building clients for Managed Applications: %+v", err)
}
client.ManagementGroups = managementgroup.NewClient(o)
if client.Maps, err = maps.NewClient(o); err != nil {
return fmt.Errorf("building clients for Maps: %+v", err)
Expand Down
31 changes: 20 additions & 11 deletions internal/services/managedapplications/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/managedapplications" // nolint: staticcheck
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/managedapplications/2021-07-01/applicationdefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedapplications/2021-07-01/applications"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
ApplicationClient *managedapplications.ApplicationsClient
ApplicationDefinitionClient *managedapplications.ApplicationDefinitionsClient
ApplicationClient *applications.ApplicationsClient
ApplicationDefinitionClient *applicationdefinitions.ApplicationDefinitionsClient
}

func NewClient(o *common.ClientOptions) *Client {
applicationClient := managedapplications.NewApplicationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&applicationClient.Client, o.ResourceManagerAuthorizer)
func NewClient(o *common.ClientOptions) (*Client, error) {
applicationClient, err := applications.NewApplicationsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Applications client: %+v", err)
}
o.Configure(applicationClient.Client, o.Authorizers.ResourceManager)

applicationDefinitionClient := managedapplications.NewApplicationDefinitionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&applicationDefinitionClient.Client, o.ResourceManagerAuthorizer)
applicationDefinitionClient, err := applicationdefinitions.NewApplicationDefinitionsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Application Definitions client: %+v", err)
}
o.Configure(applicationDefinitionClient.Client, o.Authorizers.ResourceManager)

return &Client{
ApplicationClient: &applicationClient,
ApplicationDefinitionClient: &applicationDefinitionClient,
}
ApplicationClient: applicationClient,
ApplicationDefinitionClient: applicationDefinitionClient,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedapplications/2021-07-01/applicationdefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/managedapplications/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/managedapplications/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceManagedApplicationDefinition() *pluginsdk.Resource {
Expand Down Expand Up @@ -42,21 +42,22 @@ func dataSourceManagedApplicationDefinitionRead(d *pluginsdk.ResourceData, meta
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewApplicationDefinitionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := applicationdefinitions.NewApplicationDefinitionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("failed to read %s: %+v", id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))

d.SetId(id.ID())

d.Set("name", id.ApplicationDefinitionName)
d.Set("resource_group_name", id.ResourceGroupName)
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/managedapplications" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/managedapplications/2021-07-01/applicationdefinitions"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/managedapplications/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/managedapplications/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"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 @@ -28,7 +28,7 @@ func resourceManagedApplicationDefinition() *pluginsdk.Resource {
Delete: resourceManagedApplicationDefinitionDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ApplicationDefinitionID(id)
_, err := applicationdefinitions.ParseApplicationDefinitionID(id)
return err
}),

Expand Down Expand Up @@ -62,9 +62,9 @@ func resourceManagedApplicationDefinition() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(managedapplications.CanNotDelete),
string(managedapplications.None),
string(managedapplications.ReadOnly),
string(applicationdefinitions.ApplicationLockLevelCanNotDelete),
string(applicationdefinitions.ApplicationLockLevelNone),
string(applicationdefinitions.ApplicationLockLevelReadOnly),
}, false),
},

Expand Down Expand Up @@ -94,6 +94,7 @@ func resourceManagedApplicationDefinition() *pluginsdk.Resource {
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: pluginsdk.SuppressJsonDiff,
ConflictsWith: []string{"package_file_uri"},
RequiredWith: []string{"main_template"},
},

"description": {
Expand All @@ -108,6 +109,7 @@ func resourceManagedApplicationDefinition() *pluginsdk.Resource {
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: pluginsdk.SuppressJsonDiff,
ConflictsWith: []string{"package_file_uri"},
RequiredWith: []string{"create_ui_definition"},
},

"package_enabled": {
Expand All @@ -122,7 +124,7 @@ func resourceManagedApplicationDefinition() *pluginsdk.Resource {
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand All @@ -133,55 +135,47 @@ func resourceManagedApplicationDefinitionCreateUpdate(d *pluginsdk.ResourceData,
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewApplicationDefinitionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := applicationdefinitions.NewApplicationDefinitionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("failed to check for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_managed_application_definition", id.ID())
}
}

parameters := managedapplications.ApplicationDefinition{
Location: utils.String(azure.NormalizeLocation(d.Get("location"))),
ApplicationDefinitionProperties: &managedapplications.ApplicationDefinitionProperties{
parameters := applicationdefinitions.ApplicationDefinition{
Location: pointer.To(location.Normalize(d.Get("location").(string))),
Properties: applicationdefinitions.ApplicationDefinitionProperties{
Authorizations: expandManagedApplicationDefinitionAuthorization(d.Get("authorization").(*pluginsdk.Set).List()),
Description: utils.String(d.Get("description").(string)),
DisplayName: utils.String(d.Get("display_name").(string)),
Description: pointer.To(d.Get("description").(string)),
DisplayName: pointer.To(d.Get("display_name").(string)),
IsEnabled: utils.Bool(d.Get("package_enabled").(bool)),
LockLevel: managedapplications.ApplicationLockLevel(d.Get("lock_level").(string)),
LockLevel: applicationdefinitions.ApplicationLockLevel(d.Get("lock_level").(string)),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("create_ui_definition"); ok {
parameters.CreateUIDefinition = utils.String(v.(string))
parameters.Properties.CreateUiDefinition = &v
}

if v, ok := d.GetOk("main_template"); ok {
parameters.MainTemplate = utils.String(v.(string))
}

if (parameters.CreateUIDefinition != nil && parameters.MainTemplate == nil) || (parameters.CreateUIDefinition == nil && parameters.MainTemplate != nil) {
return fmt.Errorf("if either `create_ui_definition` or `main_template` is set the other one must be too")
parameters.Properties.MainTemplate = &v
}

if v, ok := d.GetOk("package_file_uri"); ok {
parameters.PackageFileURI = utils.String(v.(string))
parameters.Properties.PackageFileUri = pointer.To(v.(string))
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters)
if err != nil {
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

this was a Future, so should this be:

Suggested change
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is no polling function in the new SDK

return fmt.Errorf("failed to create %s: %+v", id, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("failed to wait for creation of %s: %+v", id, err)
}

d.SetId(id.ID())

Expand All @@ -193,105 +187,97 @@ func resourceManagedApplicationDefinitionRead(d *pluginsdk.ResourceData, meta in
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ApplicationDefinitionID(d.Id())
id, err := applicationdefinitions.ParseApplicationDefinitionID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Managed Application Definition %q does not exist - removing from state", d.Id())
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", *id)
d.SetId("")
return nil
}
return fmt.Errorf("failed to read Managed Application Definition %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("name", id.ApplicationDefinitionName)
d.Set("resource_group_name", id.ResourceGroupName) // missing from response?

if model := resp.Model; model != nil {
p := model.Properties

d.Set("location", location.NormalizeNilable(model.Location))

if props := resp.ApplicationDefinitionProperties; props != nil {
if err := d.Set("authorization", flattenManagedApplicationDefinitionAuthorization(props.Authorizations)); err != nil {
if err := d.Set("authorization", flattenManagedApplicationDefinitionAuthorization(p.Authorizations)); err != nil {
return fmt.Errorf("setting `authorization`: %+v", err)
}
d.Set("description", props.Description)
d.Set("display_name", props.DisplayName)
d.Set("package_enabled", props.IsEnabled)
d.Set("lock_level", string(props.LockLevel))
}
d.Set("description", p.Description)
d.Set("display_name", p.DisplayName)
d.Set("package_enabled", p.IsEnabled)
d.Set("lock_level", string(p.LockLevel))

// the following are not returned from the API so lets pull it from state
if v, ok := d.GetOk("create_ui_definition"); ok {
d.Set("create_ui_definition", v.(string))
}
if v, ok := d.GetOk("main_template"); ok {
d.Set("main_template", v.(string))
}
if v, ok := d.GetOk("package_file_uri"); ok {
d.Set("package_file_uri", v.(string))
}

// the following are not returned from the API so lets pull it from state
if v, ok := d.GetOk("create_ui_definition"); ok {
d.Set("create_ui_definition", v.(string))
}
if v, ok := d.GetOk("main_template"); ok {
d.Set("main_template", v.(string))
}
if v, ok := d.GetOk("package_file_uri"); ok {
d.Set("package_file_uri", v.(string))
if err = tags.FlattenAndSet(d, model.Tags); err != nil {
return fmt.Errorf("setting `tags`: %+v", err)
}
}

return tags.FlattenAndSet(d, resp.Tags)
return nil
}

func resourceManagedApplicationDefinitionDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ManagedApplication.ApplicationDefinitionClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ApplicationDefinitionID(d.Id())
id, err := applicationdefinitions.ParseApplicationDefinitionID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("failed to delete Managed Application Definition %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("failed to wait for deleting Managed Application Definition (Managed Application Definition Name %q / Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
if _, err = client.Delete(ctx, *id); err != nil {
return fmt.Errorf("issuing AzureRM delete request for '%s': %+v", *id, err)
}

return nil
}

func expandManagedApplicationDefinitionAuthorization(input []interface{}) *[]managedapplications.ApplicationAuthorization {
results := make([]managedapplications.ApplicationAuthorization, 0)
func expandManagedApplicationDefinitionAuthorization(input []interface{}) *[]applicationdefinitions.ApplicationAuthorization {
results := make([]applicationdefinitions.ApplicationAuthorization, 0)
for _, item := range input {
v := item.(map[string]interface{})
result := managedapplications.ApplicationAuthorization{
RoleDefinitionID: utils.String(v["role_definition_id"].(string)),
PrincipalID: utils.String(v["service_principal_id"].(string)),
result := applicationdefinitions.ApplicationAuthorization{
RoleDefinitionId: v["role_definition_id"].(string),
PrincipalId: v["service_principal_id"].(string),
}

results = append(results, result)
}
return &results
}

func flattenManagedApplicationDefinitionAuthorization(input *[]managedapplications.ApplicationAuthorization) []interface{} {
func flattenManagedApplicationDefinitionAuthorization(input *[]applicationdefinitions.ApplicationAuthorization) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, item := range *input {
servicePrincipalId := ""
if item.PrincipalID != nil {
servicePrincipalId = *item.PrincipalID
}

roleDefinitionId := ""
if item.RoleDefinitionID != nil {
roleDefinitionId = *item.RoleDefinitionID
}

results = append(results, map[string]interface{}{
"role_definition_id": roleDefinitionId,
"service_principal_id": servicePrincipalId,
"role_definition_id": item.PrincipalId,
"service_principal_id": item.RoleDefinitionId,
})
}

Expand Down
Loading