-
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 resources:
azurerm_log_analytics_cluster
and `azurerm_log_analy…
…tics_cluster_customer_managed_key` (#8946) Co-authored-by: kt <[email protected]> Co-authored-by: jackofallops <[email protected]>
- Loading branch information
1 parent
e30fcf8
commit 4431082
Showing
20 changed files
with
1,581 additions
and
7 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
46 changes: 46 additions & 0 deletions
46
azurerm/internal/services/loganalytics/log_analytics_cluster.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,46 @@ | ||
package loganalytics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2020-03-01-preview/operationalinsights" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
) | ||
|
||
func logAnalyticsClusterWaitForState(ctx context.Context, meta interface{}, timeout time.Duration, resourceGroup string, clusterName string) *resource.StateChangeConf { | ||
return &resource.StateChangeConf{ | ||
Pending: []string{string(operationalinsights.Updating)}, | ||
Target: []string{string(operationalinsights.Succeeded)}, | ||
MinTimeout: 1 * time.Minute, | ||
Timeout: timeout, | ||
Refresh: logAnalyticsClusterRefresh(ctx, meta, resourceGroup, clusterName), | ||
} | ||
} | ||
|
||
func logAnalyticsClusterRefresh(ctx context.Context, meta interface{}, resourceGroup string, clusterName string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
client := meta.(*clients.Client).LogAnalytics.ClusterClient | ||
|
||
log.Printf("[INFO] checking on state of Log Analytics Cluster %q", clusterName) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, clusterName) | ||
if err != nil { | ||
return nil, "nil", fmt.Errorf("polling for the status of Log Analytics Cluster %q (Resource Group %q): %v", clusterName, resourceGroup, err) | ||
} | ||
|
||
if resp.ClusterProperties != nil { | ||
if resp.ClusterProperties.ProvisioningState != operationalinsights.Updating && resp.ClusterProperties.ProvisioningState != operationalinsights.Succeeded { | ||
return nil, "nil", fmt.Errorf("Log Analytics Cluster %q (Resource Group %q) unexpected Provisioning State encountered: %q", clusterName, resourceGroup, string(resp.ClusterProperties.ProvisioningState)) | ||
} | ||
|
||
return resp, string(resp.ClusterProperties.ProvisioningState), nil | ||
} | ||
|
||
// I am not returning an error here as this might have just been a bad get | ||
return resp, "nil", nil | ||
} | ||
} |
204 changes: 204 additions & 0 deletions
204
...erm/internal/services/loganalytics/log_analytics_cluster_customer_managed_key_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,204 @@ | ||
package loganalytics | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2020-03-01-preview/operationalinsights" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"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/services/loganalytics/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmLogAnalyticsClusterCustomerManagedKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmLogAnalyticsClusterCustomerManagedKeyCreate, | ||
Read: resourceArmLogAnalyticsClusterCustomerManagedKeyRead, | ||
Update: resourceArmLogAnalyticsClusterCustomerManagedKeyUpdate, | ||
Delete: resourceArmLogAnalyticsClusterCustomerManagedKeyDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(6 * time.Hour), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(6 * time.Hour), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"log_analytics_cluster_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.LogAnalyticsClusterId, | ||
}, | ||
|
||
"key_vault_key_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: azure.ValidateKeyVaultChildIdVersionOptional, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmLogAnalyticsClusterCustomerManagedKeyCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).LogAnalytics.ClusterClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
clusterIdRaw := d.Get("log_analytics_cluster_id").(string) | ||
clusterId, err := parse.LogAnalyticsClusterID(clusterIdRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, clusterId.ResourceGroup, clusterId.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Log Analytics Cluster %q (resource group %q) was not found", clusterId.Name, clusterId.ResourceGroup) | ||
} | ||
return fmt.Errorf("failed to get details of Log Analytics Cluster %q (resource group %q): %+v", clusterId.Name, clusterId.ResourceGroup, err) | ||
} | ||
if resp.ClusterProperties != nil && resp.ClusterProperties.KeyVaultProperties != nil { | ||
keyProps := *resp.ClusterProperties.KeyVaultProperties | ||
if keyProps.KeyName != nil && *keyProps.KeyName != "" { | ||
return tf.ImportAsExistsError("azurerm_log_analytics_cluster_customer_managed_key", fmt.Sprintf("%s/CMK", clusterIdRaw)) | ||
} | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/CMK", clusterIdRaw)) | ||
return resourceArmLogAnalyticsClusterCustomerManagedKeyUpdate(d, meta) | ||
} | ||
|
||
func resourceArmLogAnalyticsClusterCustomerManagedKeyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).LogAnalytics.ClusterClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
keyId, err := azure.ParseKeyVaultChildIDVersionOptional(d.Get("key_vault_key_id").(string)) | ||
if err != nil { | ||
return fmt.Errorf("could not parse Key Vault Key ID: %+v", err) | ||
} | ||
|
||
clusterId, err := parse.LogAnalyticsClusterID(d.Get("log_analytics_cluster_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clusterPatch := operationalinsights.ClusterPatch{ | ||
ClusterPatchProperties: &operationalinsights.ClusterPatchProperties{ | ||
KeyVaultProperties: &operationalinsights.KeyVaultProperties{ | ||
KeyVaultURI: utils.String(keyId.KeyVaultBaseUrl), | ||
KeyName: utils.String(keyId.Name), | ||
KeyVersion: utils.String(keyId.Version), | ||
}, | ||
}, | ||
} | ||
|
||
if _, err := client.Update(ctx, clusterId.ResourceGroup, clusterId.Name, clusterPatch); err != nil { | ||
return fmt.Errorf("updating Log Analytics Cluster %q (Resource Group %q): %+v", clusterId.Name, clusterId.ResourceGroup, err) | ||
} | ||
|
||
updateWait := logAnalyticsClusterWaitForState(ctx, meta, d.Timeout(schema.TimeoutUpdate), clusterId.ResourceGroup, clusterId.Name) | ||
|
||
if _, err := updateWait.WaitForState(); err != nil { | ||
return fmt.Errorf("waiting for Log Analytics Cluster to finish updating %q (Resource Group %q): %v", clusterId.Name, clusterId.ResourceGroup, err) | ||
} | ||
|
||
return resourceArmLogAnalyticsClusterCustomerManagedKeyRead(d, meta) | ||
} | ||
|
||
func resourceArmLogAnalyticsClusterCustomerManagedKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).LogAnalytics.ClusterClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
idRaw := strings.TrimRight(d.Id(), "/CMK") | ||
|
||
id, err := parse.LogAnalyticsClusterID(idRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("log_analytics_cluster_id", idRaw) | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] Log Analytics %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("retrieving Log Analytics Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
if props := resp.ClusterProperties; props != nil { | ||
if kvProps := props.KeyVaultProperties; kvProps != nil { | ||
var keyVaultUri, keyName, keyVersion string | ||
if kvProps.KeyVaultURI != nil && *kvProps.KeyVaultURI != "" { | ||
keyVaultUri = *kvProps.KeyVaultURI | ||
} else { | ||
return fmt.Errorf("empty value returned for Key Vault URI") | ||
} | ||
if kvProps.KeyName != nil && *kvProps.KeyName != "" { | ||
keyName = *kvProps.KeyName | ||
} else { | ||
return fmt.Errorf("empty value returned for Key Vault Key Name") | ||
} | ||
if kvProps.KeyVersion != nil { | ||
keyVersion = *kvProps.KeyVersion | ||
} | ||
keyVaultKeyId, err := azure.NewKeyVaultChildResourceID(keyVaultUri, "keys", keyName, keyVersion) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("key_vault_key_id", keyVaultKeyId) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmLogAnalyticsClusterCustomerManagedKeyDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).LogAnalytics.ClusterClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
clusterId, err := parse.LogAnalyticsClusterID(d.Get("log_analytics_cluster_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clusterPatch := operationalinsights.ClusterPatch{ | ||
ClusterPatchProperties: &operationalinsights.ClusterPatchProperties{ | ||
KeyVaultProperties: &operationalinsights.KeyVaultProperties{ | ||
KeyVaultURI: nil, | ||
KeyName: nil, | ||
KeyVersion: nil, | ||
}, | ||
}, | ||
} | ||
|
||
if _, err = client.Update(ctx, clusterId.ResourceGroup, clusterId.Name, clusterPatch); err != nil { | ||
return fmt.Errorf("removing Log Analytics Cluster Customer Managed Key from cluster %q (resource group %q)", clusterId.Name, clusterId.ResourceGroup) | ||
} | ||
|
||
deleteWait := logAnalyticsClusterWaitForState(ctx, meta, d.Timeout(schema.TimeoutDelete), clusterId.ResourceGroup, clusterId.Name) | ||
|
||
if _, err := deleteWait.WaitForState(); err != nil { | ||
return fmt.Errorf("waiting for Log Analytics Cluster to finish updating %q (Resource Group %q): %v", clusterId.Name, clusterId.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.