Skip to content

Commit

Permalink
new resource azurerm_graph_account (#22334)
Browse files Browse the repository at this point in the history
* 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
teowa authored Jul 18, 2023
1 parent be71186 commit 08c72f7
Show file tree
Hide file tree
Showing 29 changed files with 1,464 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ service/fluid-relay:
service/frontdoor:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_frontdoor((.|\n)*)###'

service/graph:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_graph_account((.|\n)*)###'

service/hdinsight:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_hdinsight_((.|\n)*)###'

Expand Down
3 changes: 3 additions & 0 deletions .github/labeler-pull-request-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ service/fluid-relay:
service/frontdoor:
- internal/services/frontdoor/**/*

service/graph:
- internal/services/graph/**/*

service/hdinsight:
- internal/services/hdinsight/**/*

Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var services = mapOf(
"firewall" to "Firewall",
"fluidrelay" to "Fluid Relay",
"frontdoor" to "FrontDoor",
"graph" to "Graph",
"hdinsight" to "HDInsight",
"hpccache" to "HPC Cache",
"hsm" to "Hardware Security Module",
Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import (
firewall "github.com/hashicorp/terraform-provider-azurerm/internal/services/firewall/client"
fluidrelay "github.com/hashicorp/terraform-provider-azurerm/internal/services/fluidrelay/client"
frontdoor "github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/client"
graph "github.com/hashicorp/terraform-provider-azurerm/internal/services/graph/client"
hdinsight "github.com/hashicorp/terraform-provider-azurerm/internal/services/hdinsight/client"
healthcare "github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/client"
hpccache "github.com/hashicorp/terraform-provider-azurerm/internal/services/hpccache/client"
Expand Down Expand Up @@ -196,6 +197,7 @@ type Client struct {
Firewall *firewall.Client
FluidRelay *fluidrelay_2022_05_26.Client
Frontdoor *frontdoor.Client
Graph *graph.Client
HPCCache *hpccache.Client
HSM *hsm.Client
HDInsight *hdinsight.Client
Expand Down Expand Up @@ -388,6 +390,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
return fmt.Errorf("building clients for FluidRelay: %+v", err)
}
client.Frontdoor = frontdoor.NewClient(o)
if client.Graph, err = graph.NewClient(o); err != nil {
return fmt.Errorf("building clients for Graph: %+v", err)
}
client.HPCCache = hpccache.NewClient(o)
client.HSM = hsm.NewClient(o)
client.HDInsight = hdinsight.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/firewall"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/fluidrelay"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/graph"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/hdinsight"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/hpccache"
Expand Down Expand Up @@ -155,6 +156,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
domainservices.Registration{},
eventhub.Registration{},
fluidrelay.Registration{},
graph.Registration{},
hybridcompute.Registration{},
iothub.Registration{},
iotcentral.Registration{},
Expand Down
27 changes: 27 additions & 0 deletions internal/services/graph/client/client.go
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
}
209 changes: 209 additions & 0 deletions internal/services/graph/graph_account_resource.go
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
},
}
}
Loading

0 comments on commit 08c72f7

Please sign in to comment.