Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: Kusto Attached Database Configuration
- Loading branch information
1 parent
5e66b05
commit 94eba6f
Showing
8 changed files
with
578 additions
and
10 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
220 changes: 220 additions & 0 deletions
220
azurerm/internal/services/kusto/kusto_attached_database_configuration.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,220 @@ | ||
package kusto | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/kusto/mgmt/2020-02-15/kusto" | ||
"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/services/kusto/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmKustoAttachedDatabaseConfiguration() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmKustoAttachedDatabaseConfigurationCreateUpdate, | ||
Read: resourceArmKustoAttachedDatabaseConfigurationRead, | ||
Update: resourceArmKustoAttachedDatabaseConfigurationCreateUpdate, | ||
Delete: resourceArmKustoAttachedDatabaseConfigurationDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(60 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(60 * time.Minute), | ||
Delete: schema.DefaultTimeout(60 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAzureRMKustoDataConnectionName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"cluster_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAzureRMKustoClusterName, | ||
}, | ||
|
||
"database_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.Any(validateAzureRMKustoDatabaseName, validation.StringInSlice([]string{"*"}, false)), | ||
}, | ||
|
||
"cluster_resource_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
|
||
"attached_database_names": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
|
||
"default_principal_modification_kind": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: kusto.DefaultPrincipalsModificationKindNone, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(kusto.DefaultPrincipalsModificationKindNone), | ||
string(kusto.DefaultPrincipalsModificationKindReplace), | ||
string(kusto.DefaultPrincipalsModificationKindUnion), | ||
}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmKustoAttachedDatabaseConfigurationCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Kusto.AttachedDatabaseConfigurationsClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Printf("[INFO] preparing arguments for Azure Kusto Attached Database Configuration creation.") | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
clusterName := d.Get("cluster_name").(string) | ||
|
||
if d.IsNewResource() { | ||
resp, err := client.Get(ctx, resourceGroup, clusterName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error checking for presence of existing Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %s", name, resourceGroup, clusterName, err) | ||
} | ||
} | ||
|
||
if resp.ID != nil && *resp.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_kusto_attached_database_configuration", *resp.ID) | ||
} | ||
} | ||
|
||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||
|
||
configurationProperties := expandKustoAttachedDatabaseConfigurationProperties(d) | ||
|
||
configurationRequest := kusto.AttachedDatabaseConfiguration{ | ||
Location: &location, | ||
AttachedDatabaseConfigurationProperties: configurationProperties, | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, clusterName, name, configurationRequest) | ||
if err != nil { | ||
return fmt.Errorf("Error creating or updating Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", name, resourceGroup, clusterName, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for completion of Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", name, resourceGroup, clusterName, err) | ||
} | ||
|
||
configuration, err := client.Get(ctx, resourceGroup, clusterName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", name, resourceGroup, clusterName, err) | ||
} | ||
|
||
d.SetId(*configuration.ID) | ||
|
||
return resourceArmKustoAttachedDatabaseConfigurationRead(d, meta) | ||
} | ||
|
||
func resourceArmKustoAttachedDatabaseConfigurationRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Kusto.AttachedDatabaseConfigurationsClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.KustoAttachedDatabaseConfigurationID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
configuration, err := client.Get(ctx, id.ResourceGroup, id.Cluster, id.Name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(configuration.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error retrieving Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", id.Name, id.ResourceGroup, id.Cluster, err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
d.Set("cluster_name", id.Cluster) | ||
|
||
if location := configuration.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
if props := configuration.AttachedDatabaseConfigurationProperties; props != nil { | ||
d.Set("cluster_resource_id", props.ClusterResourceID) | ||
d.Set("database_name", props.DatabaseName) | ||
d.Set("default_principal_modification_kind", props.DefaultPrincipalsModificationKind) | ||
d.Set("attached_database_names", props.AttachedDatabaseNames) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmKustoAttachedDatabaseConfigurationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Kusto.AttachedDatabaseConfigurationsClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.KustoAttachedDatabaseConfigurationID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
future, err := client.Delete(ctx, id.ResourceGroup, id.Cluster, id.Name) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", id.Name, id.ResourceGroup, id.Cluster, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for deletion of Kusto Attached Database Configuration %q (Resource Group %q, Cluster %q): %+v", id.Name, id.ResourceGroup, id.Cluster, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandKustoAttachedDatabaseConfigurationProperties(d *schema.ResourceData) *kusto.AttachedDatabaseConfigurationProperties { | ||
AttachedDatabaseConfigurationProperties := &kusto.AttachedDatabaseConfigurationProperties{} | ||
|
||
if clusterResourceID, ok := d.GetOk("cluster_resource_id"); ok { | ||
AttachedDatabaseConfigurationProperties.ClusterResourceID = utils.String(clusterResourceID.(string)) | ||
} | ||
|
||
if databaseName, ok := d.GetOk("database_name"); ok { | ||
AttachedDatabaseConfigurationProperties.DatabaseName = utils.String(databaseName.(string)) | ||
} | ||
|
||
if defaultPrincipalModificationKind, ok := d.GetOk("default_principal_modification_kind"); ok { | ||
AttachedDatabaseConfigurationProperties.DefaultPrincipalsModificationKind = kusto.DefaultPrincipalsModificationKind(defaultPrincipalModificationKind.(string)) | ||
} | ||
|
||
return AttachedDatabaseConfigurationProperties | ||
} |
38 changes: 38 additions & 0 deletions
38
azurerm/internal/services/kusto/parse/kusto_attached_database_configuration.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,38 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type KustoAttachedDatabaseConfigurationId struct { | ||
ResourceGroup string | ||
Cluster string | ||
Name string | ||
} | ||
|
||
func KustoAttachedDatabaseConfigurationID(input string) (*KustoAttachedDatabaseConfigurationId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERROR] Unable to parse Kusto Attached Database Configuration ID %q: %+v", input, err) | ||
} | ||
|
||
configuration := KustoAttachedDatabaseConfigurationId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if configuration.Cluster, err = id.PopSegment("Clusters"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if configuration.Name, err = id.PopSegment("AttachedDatabaseConfigurations"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &configuration, nil | ||
} |
54 changes: 54 additions & 0 deletions
54
azurerm/internal/services/kusto/parse/kusto_attached_database_configuration_test.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,54 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestKustoAttachedDatabaseConfigurationId(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *KustoAttachedDatabaseConfigurationId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Cluster", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Kusto/AttachedDatabaseConfigurations/configuration1", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Kusto Attached Database Configuration ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Kusto/Clusters/cluster1/AttachedDatabaseConfigurations/configuration1", | ||
Expected: &KustoAttachedDatabaseConfigurationId{ | ||
Name: "configuration1", | ||
Cluster: "cluster1", | ||
ResourceGroup: "group1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := KustoAttachedDatabaseConfigurationID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.