-
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.
azurerm_mysql_flexible_server_configuration
: New resource to suppor…
…t configuration on `mysql_flexible_server` (#13831) Fixes #13824 Functionality, docs and code is 99% copy of azurerm_mysql_configuration but for azurerm_mysql_flexible_server instead of azurerm_mysql_server.
- Loading branch information
Showing
10 changed files
with
763 additions
and
18 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
153 changes: 153 additions & 0 deletions
153
internal/services/mysql/mysql_flexible_server_configuration_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,153 @@ | ||
package mysql | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/mysql/mgmt/2021-05-01-preview/mysqlflexibleservers" | ||
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
func resourceMySQLFlexibleServerConfiguration() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Create: resourceMySQLFlexibleServerConfigurationCreate, | ||
Read: resourceMySQLFlexibleServerConfigurationRead, | ||
Delete: resourceMySQLFlexibleServerConfigurationDelete, | ||
|
||
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { | ||
_, err := parse.FlexibleServerConfigurationID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Create: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
Update: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
Delete: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"server_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.ServerName, | ||
}, | ||
|
||
"value": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMySQLFlexibleServerConfigurationCreate(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MySQL.FlexibleServerConfigurationsClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Printf("[INFO] preparing arguments for AzureRM MySQL Configuration creation.") | ||
|
||
properties := mysqlflexibleservers.Configuration{ | ||
ConfigurationProperties: &mysqlflexibleservers.ConfigurationProperties{ | ||
Value: utils.String(d.Get("value").(string)), | ||
}, | ||
} | ||
|
||
// NOTE: this resource intentionally doesn't support Requires Import | ||
// since a fallback route is created by default | ||
|
||
id := parse.NewFlexibleServerConfigurationID(subscriptionId, d.Get("resource_group_name").(string), d.Get("server_name").(string), d.Get("name").(string)) | ||
future, err := client.Update(ctx, id.ResourceGroup, id.FlexibleServerName, id.ConfigurationName, properties) | ||
if err != nil { | ||
return fmt.Errorf("creating %s: %v", id, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for creation of %s: %v", id, err) | ||
} | ||
|
||
d.SetId(id.ID()) | ||
return resourceMySQLFlexibleServerConfigurationRead(d, meta) | ||
} | ||
|
||
func resourceMySQLFlexibleServerConfigurationRead(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MySQL.FlexibleServerConfigurationsClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FlexibleServerConfigurationID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.FlexibleServerName, id.ConfigurationName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[WARN] %s was not found", id) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
d.Set("name", id.ConfigurationName) | ||
d.Set("server_name", id.FlexibleServerName) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
value := "" | ||
if props := resp.ConfigurationProperties; props != nil && props.Value != nil { | ||
value = *props.Value | ||
} | ||
d.Set("value", value) | ||
|
||
return nil | ||
} | ||
|
||
func resourceMySQLFlexibleServerConfigurationDelete(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MySQL.FlexibleServerConfigurationsClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FlexibleServerConfigurationID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
// "delete" = resetting this to the default value | ||
resp, err := client.Get(ctx, id.ResourceGroup, id.FlexibleServerName, id.ConfigurationName) | ||
if err != nil { | ||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
properties := mysqlflexibleservers.Configuration{ | ||
ConfigurationProperties: &mysqlflexibleservers.ConfigurationProperties{ | ||
// we can alternatively set `source: "system-default"` | ||
Value: resp.DefaultValue, | ||
}, | ||
} | ||
|
||
future, err := client.Update(ctx, id.ResourceGroup, id.FlexibleServerName, id.ConfigurationName, properties) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return future.WaitForCompletionRef(ctx, client.Client) | ||
} |
Oops, something went wrong.