-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
New resource: azurerm_hci_cluster
#9134
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d094c11
New resource azurerm_hci_cluster
a723ed1
update code
9949171
Merge remote-tracking branch 'upstream/master' into azurestackhci
2d947f6
update code
806292c
update code
ab1487f
update code
0eda876
update code
f81fa6f
update code
c201418
update code
ce1fcff
update code
2d3a247
update code
56a9600
update code
4d50408
update tc
1a05691
update code
c296f72
Merge remote-tracking branch 'upstream/master' into azurestackhci
63ec3c2
update code
885f6da
update cod
d82fe48
update code
42e8231
update code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/services/azurestackhci/mgmt/2020-10-01/azurestackhci" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common" | ||
) | ||
|
||
type Client struct { | ||
ClusterClient *azurestackhci.ClustersClient | ||
} | ||
|
||
func NewClient(o *common.ClientOptions) *Client { | ||
clusterClient := azurestackhci.NewClustersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) | ||
o.ConfigureClient(&clusterClient.Client, o.ResourceManagerAuthorizer) | ||
|
||
return &Client{ | ||
ClusterClient: &clusterClient, | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
azurerm/internal/services/azurestackhci/hci_cluster_resource.go
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,183 @@ | ||
package azurestackhci | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/azurestackhci/mgmt/2020-10-01/azurestackhci" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/azurestackhci/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/azurestackhci/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmHCICluster() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmHCIClusterCreate, | ||
Read: resourceArmHCIClusterRead, | ||
Update: resourceArmHCIClusterUpdate, | ||
Delete: resourceArmHCIClusterDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := parse.ClusterID(id) | ||
return err | ||
}), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.ClusterName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"client_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IsUUID, | ||
}, | ||
|
||
"tenant_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IsUUID, | ||
}, | ||
|
||
"tags": tags.Schema(), | ||
}, | ||
} | ||
} | ||
func resourceArmHCIClusterCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AzureStackHCI.ClusterClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
id := parse.NewClusterID(subscriptionId, resourceGroup, name) | ||
|
||
existing, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for present of existing HCI Cluster %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_hci_cluster", *existing.ID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be using the Resource ID formatter's ID here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
|
||
cluster := azurestackhci.Cluster{ | ||
Location: utils.String(location.Normalize(d.Get("location").(string))), | ||
ClusterProperties: &azurestackhci.ClusterProperties{ | ||
AadClientID: utils.String(d.Get("client_id").(string)), | ||
AadTenantID: utils.String(d.Get("tenant_id").(string)), | ||
}, | ||
Tags: tags.Expand(d.Get("tags").(map[string]interface{})), | ||
} | ||
|
||
if _, err := client.Create(ctx, resourceGroup, name, cluster); err != nil { | ||
return fmt.Errorf("creating HCI Cluster %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.SetId(id.ID("")) | ||
|
||
return resourceArmHCIClusterRead(d, meta) | ||
} | ||
|
||
func resourceArmHCIClusterRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AzureStackHCI.ClusterClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.ClusterID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] HCI Cluster %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("retrieving HCI Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
d.Set("location", location.NormalizeNilable(resp.Location)) | ||
|
||
if props := resp.ClusterProperties; props != nil { | ||
d.Set("client_id", props.AadClientID) | ||
d.Set("tenant_id", props.AadTenantID) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
func resourceArmHCIClusterUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AzureStackHCI.ClusterClient | ||
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.ClusterID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cluster := azurestackhci.ClusterUpdate{} | ||
|
||
if d.HasChange("tags") { | ||
cluster.Tags = tags.Expand(d.Get("tags").(map[string]interface{})) | ||
} | ||
|
||
if _, err := client.Update(ctx, id.ResourceGroup, id.Name, cluster); err != nil { | ||
return fmt.Errorf("updating HCI Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
return resourceArmHCIClusterRead(d, meta) | ||
} | ||
|
||
func resourceArmHCIClusterDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).AzureStackHCI.ClusterClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.ClusterID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.Delete(ctx, id.ResourceGroup, id.Name); err != nil { | ||
return fmt.Errorf("deleting HCI Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this statistically likely to be the current Tenant ID, so shouldn't this be Optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional (and Computed*) and ForceNew, actually - since this could be defaulted and overwritten
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated