-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new resource
azurerm_graph_account
(#22334)
* new resource `azurerm_graph_account` * complement * new resource `azurerm_graph_account` * complement * fix doc * add empty line * use principal for test * refine * generate
- Loading branch information
Showing
29 changed files
with
1,464 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
graphservicesV20230413 "github.com/hashicorp/go-azure-sdk/resource-manager/graphservices/2023-04-13" | ||
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/common" | ||
) | ||
|
||
type Client struct { | ||
V20230413 *graphservicesV20230413.Client | ||
} | ||
|
||
func NewClient(o *common.ClientOptions) (*Client, error) { | ||
|
||
v20230413Client, err := graphservicesV20230413.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) { | ||
o.Configure(c, o.Authorizers.ResourceManager) | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("building client for graphservices V20230413: %+v", err) | ||
} | ||
|
||
return &Client{ | ||
V20230413: v20230413Client, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package graph | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"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/go-azure-helpers/resourcemanager/tags" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/graphservices/2023-04-13/graphservicesprods" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
var _ sdk.Resource = AccountResource{} | ||
var _ sdk.ResourceWithUpdate = AccountResource{} | ||
|
||
type AccountResource struct{} | ||
|
||
func (r AccountResource) ModelObject() interface{} { | ||
return &AccountResourceSchema{} | ||
} | ||
|
||
type AccountResourceSchema struct { | ||
ApplicationId string `tfschema:"application_id"` | ||
BillingPlanId string `tfschema:"billing_plan_id"` | ||
Name string `tfschema:"name"` | ||
ResourceGroupName string `tfschema:"resource_group_name"` | ||
Tags map[string]interface{} `tfschema:"tags"` | ||
} | ||
|
||
func (r AccountResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return graphservicesprods.ValidateAccountID | ||
} | ||
|
||
func (r AccountResource) ResourceType() string { | ||
return "azurerm_graph_account" | ||
} | ||
|
||
func (r AccountResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"resource_group_name": commonschema.ResourceGroupName(), | ||
"application_id": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: validation.IsUUID, | ||
}, | ||
"tags": commonschema.Tags(), | ||
} | ||
} | ||
|
||
func (r AccountResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"billing_plan_id": { | ||
Computed: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
} | ||
} | ||
|
||
func (r AccountResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Graph.V20230413.Graphservicesprods | ||
|
||
var config AccountResourceSchema | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
id := graphservicesprods.NewAccountID(subscriptionId, config.ResourceGroupName, config.Name) | ||
|
||
existing, err := client.AccountsGet(ctx, id) | ||
if err != nil { | ||
if !response.WasNotFound(existing.HttpResponse) { | ||
return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err) | ||
} | ||
} | ||
if !response.WasNotFound(existing.HttpResponse) { | ||
return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
} | ||
|
||
payload := graphservicesprods.AccountResource{ | ||
Location: pointer.To(location.Normalize("global")), | ||
Tags: tags.Expand(config.Tags), | ||
Properties: graphservicesprods.AccountResourceProperties{ | ||
AppId: config.ApplicationId, | ||
}, | ||
} | ||
|
||
if err := client.AccountsCreateAndUpdateThenPoll(ctx, id, payload); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r AccountResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Graph.V20230413.Graphservicesprods | ||
|
||
id, err := graphservicesprods.ParseAccountID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.AccountsGet(ctx, *id) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return metadata.MarkAsGone(*id) | ||
} | ||
return fmt.Errorf("retrieving %s: %+v", *id, err) | ||
} | ||
|
||
model := resp.Model | ||
if model == nil { | ||
return fmt.Errorf("retrieving %s: model was nil", *id) | ||
} | ||
|
||
schema := AccountResourceSchema{ | ||
ApplicationId: model.Properties.AppId, | ||
Name: id.AccountName, | ||
ResourceGroupName: id.ResourceGroupName, | ||
Tags: tags.Flatten(model.Tags), | ||
} | ||
if model.Properties.BillingPlanId != nil { | ||
schema.BillingPlanId = pointer.From(model.Properties.BillingPlanId) | ||
} | ||
|
||
return metadata.Encode(&schema) | ||
}, | ||
} | ||
} | ||
|
||
func (r AccountResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Graph.V20230413.Graphservicesprods | ||
|
||
id, err := graphservicesprods.ParseAccountID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.AccountsDelete(ctx, *id); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r AccountResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Graph.V20230413.Graphservicesprods | ||
|
||
id, err := graphservicesprods.ParseAccountID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var config AccountResourceSchema | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
existing, err := client.AccountsGet(ctx, *id) | ||
if err != nil { | ||
return fmt.Errorf("retrieving existing %s: %+v", *id, err) | ||
} | ||
if existing.Model == nil { | ||
return fmt.Errorf("retrieving existing %s: model was nil", *id) | ||
} | ||
payload := *existing.Model | ||
|
||
if metadata.ResourceData.HasChange("tags") { | ||
payload.Tags = tags.Expand(config.Tags) | ||
} | ||
|
||
if err := client.AccountsCreateAndUpdateThenPoll(ctx, *id, payload); err != nil { | ||
return fmt.Errorf("updating %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
Oops, something went wrong.