Skip to content

Commit

Permalink
New Resource - azurerm_fluid_relay_server (#17238)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 authored Jul 7, 2022
1 parent ba4451a commit 957f153
Show file tree
Hide file tree
Showing 44 changed files with 2,712 additions and 0 deletions.
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var services = mapOf(
"eventgrid" to "EventGrid",
"eventhub" to "EventHub",
"firewall" to "Firewall",
"fluidrelay" to "Fluid Relay",
"frontdoor" to "FrontDoor",
"hdinsight" to "HDInsight",
"hpccache" to "HPC Cache",
Expand Down
3 changes: 3 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
eventgrid "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid/client"
eventhub "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub/client"
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"
hdinsight "github.com/hashicorp/terraform-provider-azurerm/internal/services/hdinsight/client"
healthcare "github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/client"
Expand Down Expand Up @@ -159,6 +160,7 @@ type Client struct {
EventGrid *eventgrid.Client
Eventhub *eventhub.Client
Firewall *firewall.Client
FluidRelay *fluidrelay.Client
Frontdoor *frontdoor.Client
HPCCache *hpccache.Client
HSM *hsm.Client
Expand Down Expand Up @@ -272,6 +274,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.EventGrid = eventgrid.NewClient(o)
client.Eventhub = eventhub.NewClient(o)
client.Firewall = firewall.NewClient(o)
client.FluidRelay = fluidrelay.NewClient(o)
client.Frontdoor = frontdoor.NewClient(o)
client.HPCCache = hpccache.NewClient(o)
client.HSM = hsm.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 @@ -44,6 +44,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub"
"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/hdinsight"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare"
Expand Down Expand Up @@ -122,6 +123,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
disks.Registration{},
domainservices.Registration{},
eventhub.Registration{},
fluidrelay.Registration{},
keyvault.Registration{},
loadbalancer.Registration{},
loadtest.Registration{},
Expand Down
19 changes: 19 additions & 0 deletions internal/services/fluidrelay/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

import (
servers "github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26/fluidrelayservers"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
ServerClient *servers.FluidRelayServersClient
}

func NewClient(o *common.ClientOptions) *Client {
serverClient := servers.NewFluidRelayServersClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&serverClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ServerClient: &serverClient,
}
}
272 changes: 272 additions & 0 deletions internal/services/fluidrelay/fluid_relay_servers_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package fluidrelay

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26/fluidrelayservers"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/fluidrelay/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/utils"
)

type ServerModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
StorageSKU string `tfschema:"storage_sku"`
FrsTenantId string `tfschema:"frs_tenant_id"`
OrdererEndpoints []string `tfschema:"orderer_endpoints"`
StorageEndpoints []string `tfschema:"storage_endpoints"`
Tags map[string]string `tfschema:"tags"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
}

func (s *ServerModel) flattenIdentity(input *identity.SystemAndUserAssignedMap) error {
if input == nil {
return nil
}
config := identity.SystemAndUserAssignedMap{
Type: input.Type,
PrincipalId: input.PrincipalId,
TenantId: input.TenantId,
IdentityIds: make(map[string]identity.UserAssignedIdentityDetails),
}
for k, v := range input.IdentityIds {
config.IdentityIds[k] = identity.UserAssignedIdentityDetails{
ClientId: v.ClientId,
PrincipalId: v.PrincipalId,
}
}
model, err := identity.FlattenSystemAndUserAssignedMapToModel(&config)
if err != nil {
return err
}
s.Identity = *model
return nil
}

type Server struct{}

var _ sdk.ResourceWithUpdate = (*Server)(nil)

func (s Server) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.FluidRelayServerName,
},
"resource_group_name": commonschema.ResourceGroupName(),
"location": commonschema.Location(),
"tags": commonschema.Tags(),
"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),
"storage_sku": {
// todo remove computed when https://github.com/Azure/azure-rest-api-specs/issues/19700 is fixed
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(fluidrelayservers.PossibleValuesForStorageSKU(), false),
},
}
}

func (s Server) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"frs_tenant_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"orderer_endpoints": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"storage_endpoints": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (s Server) ModelObject() interface{} {
return &ServerModel{}
}

func (s Server) ResourceType() string {
return "azurerm_fluid_relay_server"
}

func (s Server) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) (err error) {
client := meta.Client.FluidRelay.ServerClient

var model ServerModel
if err = meta.Decode(&model); err != nil {
return err
}

subscriptionID := meta.Client.Account.SubscriptionId
id := fluidrelayservers.NewFluidRelayServerID(subscriptionID, model.ResourceGroup, model.Name)

existing, err := client.Get(ctx, id)
if !response.WasNotFound(existing.HttpResponse) {
if err != nil {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}

return meta.ResourceRequiresImport(s.ResourceType(), id)
}

serverReq := fluidrelayservers.FluidRelayServer{
Location: azure.NormalizeLocation(model.Location),
Name: utils.String(model.Name),
}
if model.Tags != nil {
serverReq.Tags = &model.Tags
}
serverReq.Properties = &fluidrelayservers.FluidRelayServerProperties{}
serverReq.Identity, err = identity.ExpandSystemAndUserAssignedMapFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding user identities: %+v", err)
}

if model.StorageSKU != "" {
serverReq.Properties.Storagesku = (*fluidrelayservers.StorageSKU)(&model.StorageSKU)
}
_, err = client.CreateOrUpdate(ctx, id, serverReq)
if err != nil {
return fmt.Errorf("creating %v err: %+v", id, err)
}
meta.SetID(id)

return nil
},
}
}

func (s Server) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 10 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) (err error) {
client := meta.Client.FluidRelay.ServerClient
id, err := fluidrelayservers.ParseFluidRelayServerID(meta.ResourceData.Id())
if err != nil {
return err
}

var model ServerModel
if err = meta.Decode(&model); err != nil {
return fmt.Errorf("decoding err: %+v", err)
}

var upd fluidrelayservers.FluidRelayServerUpdate
if meta.ResourceData.HasChange("tags") {
upd.Tags = &model.Tags
}
if meta.ResourceData.HasChange("identity") {
upd.Identity, err = identity.ExpandSystemAndUserAssignedMapFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding user identities: %+v", err)
}
}
if _, err = client.Update(ctx, *id, upd); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
}

return nil
},
}
}

func (s Server) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.FluidRelay.ServerClient

id, err := fluidrelayservers.ParseFluidRelayServerID(meta.ResourceData.Id())
if err != nil {
return err
}

server, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %v", id, err)
}

model := server.Model

output := &ServerModel{
Name: id.FluidRelayServerName,
ResourceGroup: id.ResourceGroup,
Location: location.Normalize(model.Location),
}
if err = output.flattenIdentity(model.Identity); err != nil {
return fmt.Errorf("flattening `identity`: %v", err)
}
if server.Model.Tags != nil {
output.Tags = *server.Model.Tags
}
if prop := model.Properties; prop != nil {
if prop.FrsTenantId != nil {
output.FrsTenantId = *prop.FrsTenantId
}
if points := prop.FluidRelayEndpoints; points != nil {
if points.OrdererEndpoints != nil {
output.OrdererEndpoints = *points.OrdererEndpoints
}
if points.StorageEndpoints != nil {
output.StorageEndpoints = *points.StorageEndpoints
}
}
}
if val, ok := meta.ResourceData.GetOk("storage_sku"); ok {
output.StorageSKU = val.(string)
}
return meta.Encode(output)
},
}
}

func (s Server) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 10 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.FluidRelay.ServerClient

id, err := fluidrelayservers.ParseFluidRelayServerID(meta.ResourceData.Id())
if err != nil {
return err
}

meta.Logger.Infof("deleting %s", id)
if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("delete %s: %v", id, err)
}
return nil
},
}
}

func (s Server) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return fluidrelayservers.ValidateFluidRelayServerID
}
Loading

0 comments on commit 957f153

Please sign in to comment.