Skip to content

Commit

Permalink
Merge pull request #740 from terraform-providers/sdk-migration-postgr…
Browse files Browse the repository at this point in the history
…esql

SDKv12 postgresql migration
  • Loading branch information
tombuildsstuff authored Jan 23, 2018
2 parents 729811e + 1c9080b commit 103811f
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 2,289 deletions.
22 changes: 5 additions & 17 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/operationalinsights"
"github.com/Azure/azure-sdk-for-go/arm/postgresql"
keyVault "github.com/Azure/azure-sdk-for-go/dataplane/keyvault"
appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/authorization/mgmt/2015-07-01/authorization"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/azure-sdk-for-go/services/mysql/mgmt/2017-04-30-preview/mysql"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2016-04-01/redis"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks"
Expand Down Expand Up @@ -646,31 +646,19 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto

// PostgreSQL
postgresqlConfigClient := postgresql.NewConfigurationsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&postgresqlConfigClient.Client)
postgresqlConfigClient.Authorizer = auth
postgresqlConfigClient.Sender = autorest.CreateSender(withRequestLogging())
postgresqlConfigClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.configureClient(&postgresqlConfigClient.Client, auth)
c.postgresqlConfigurationsClient = postgresqlConfigClient

postgresqlDBClient := postgresql.NewDatabasesClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&postgresqlDBClient.Client)
postgresqlDBClient.Authorizer = auth
postgresqlDBClient.Sender = autorest.CreateSender(withRequestLogging())
postgresqlDBClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.configureClient(&postgresqlDBClient.Client, auth)
c.postgresqlDatabasesClient = postgresqlDBClient

postgresqlFWClient := postgresql.NewFirewallRulesClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&postgresqlFWClient.Client)
postgresqlFWClient.Authorizer = auth
postgresqlFWClient.Sender = autorest.CreateSender(withRequestLogging())
postgresqlFWClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.configureClient(&postgresqlFWClient.Client, auth)
c.postgresqlFirewallRulesClient = postgresqlFWClient

postgresqlSrvClient := postgresql.NewServersClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&postgresqlSrvClient.Client)
postgresqlSrvClient.Authorizer = auth
postgresqlSrvClient.Sender = autorest.CreateSender(withRequestLogging())
postgresqlSrvClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.configureClient(&postgresqlSrvClient.Client, auth)
c.postgresqlServersClient = postgresqlSrvClient

// SQL Azure
Expand Down
40 changes: 31 additions & 9 deletions azurerm/resource_arm_postgresql_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/arm/postgresql"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -44,6 +45,7 @@ func resourceArmPostgreSQLConfiguration() *schema.Resource {

func resourceArmPostgreSQLConfigurationCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlConfigurationsClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM PostgreSQL Configuration creation.")

Expand All @@ -59,13 +61,17 @@ func resourceArmPostgreSQLConfigurationCreateUpdate(d *schema.ResourceData, meta
},
}

_, error := client.CreateOrUpdate(resGroup, serverName, name, properties, make(chan struct{}))
err := <-error
future, err := client.CreateOrUpdate(ctx, resGroup, serverName, name, properties)
if err != nil {
return err
}

read, err := client.Get(resGroup, serverName, name)
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return err
}

read, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
return err
}
Expand All @@ -80,6 +86,7 @@ func resourceArmPostgreSQLConfigurationCreateUpdate(d *schema.ResourceData, meta

func resourceArmPostgreSQLConfigurationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlConfigurationsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -89,7 +96,7 @@ func resourceArmPostgreSQLConfigurationRead(d *schema.ResourceData, meta interfa
serverName := id.Path["servers"]
name := id.Path["configurations"]

resp, err := client.Get(resGroup, serverName, name)
resp, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] PostgreSQL Configuration '%s' was not found (resource group '%s')", name, resGroup)
Expand All @@ -110,6 +117,7 @@ func resourceArmPostgreSQLConfigurationRead(d *schema.ResourceData, meta interfa

func resourceArmPostgreSQLConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlConfigurationsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -120,7 +128,7 @@ func resourceArmPostgreSQLConfigurationDelete(d *schema.ResourceData, meta inter
name := id.Path["configurations"]

// "delete" = resetting this to the default value
resp, err := client.Get(resGroup, serverName, name)
resp, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
return fmt.Errorf("Error retrieving Postgresql Configuration '%s': %+v", name, err)
}
Expand All @@ -132,7 +140,21 @@ func resourceArmPostgreSQLConfigurationDelete(d *schema.ResourceData, meta inter
},
}

_, error := client.CreateOrUpdate(resGroup, serverName, name, properties, make(chan struct{}))
err = <-error
return err
future, err := client.CreateOrUpdate(ctx, resGroup, serverName, name, properties)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

return nil
}
9 changes: 6 additions & 3 deletions azurerm/resource_arm_postgresql_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func testCheckAzureRMPostgreSQLConfigurationValue(resourceName string, value str
}

client := testAccProvider.Meta().(*ArmClient).postgresqlConfigurationsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.Get(resourceGroup, serverName, name)
resp, err := client.Get(ctx, resourceGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: PostgreSQL Configuration %q (server %q resource group: %q) does not exist", name, serverName, resourceGroup)
Expand All @@ -136,8 +137,9 @@ func testCheckAzureRMPostgreSQLConfigurationValueReset(rInt int, configurationNa
serverName := fmt.Sprintf("acctestpsqlsvr-%d", rInt)

client := testAccProvider.Meta().(*ArmClient).postgresqlConfigurationsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.Get(resourceGroup, serverName, configurationName)
resp, err := client.Get(ctx, resourceGroup, serverName, configurationName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: PostgreSQL Configuration %q (server %q resource group: %q) does not exist", configurationName, serverName, resourceGroup)
Expand All @@ -158,6 +160,7 @@ func testCheckAzureRMPostgreSQLConfigurationValueReset(rInt int, configurationNa

func testCheckAzureRMPostgreSQLConfigurationDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).postgresqlConfigurationsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_postgresql_configuration" {
Expand All @@ -168,7 +171,7 @@ func testCheckAzureRMPostgreSQLConfigurationDestroy(s *terraform.State) error {
serverName := rs.Primary.Attributes["server_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := client.Get(resourceGroup, serverName, name)
resp, err := client.Get(ctx, resourceGroup, serverName, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
Expand Down
37 changes: 29 additions & 8 deletions azurerm/resource_arm_postgresql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/arm/postgresql"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -50,6 +51,7 @@ func resourceArmPostgreSQLDatabase() *schema.Resource {

func resourceArmPostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlDatabasesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM PostgreSQL Database creation.")

Expand All @@ -67,13 +69,17 @@ func resourceArmPostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{
},
}

_, error := client.CreateOrUpdate(resGroup, serverName, name, properties, make(chan struct{}))
err := <-error
future, err := client.CreateOrUpdate(ctx, resGroup, serverName, name, properties)
if err != nil {
return err
}

read, err := client.Get(resGroup, serverName, name)
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return err
}

read, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
return err
}
Expand All @@ -88,6 +94,7 @@ func resourceArmPostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{

func resourceArmPostgreSQLDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlDatabasesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -97,7 +104,7 @@ func resourceArmPostgreSQLDatabaseRead(d *schema.ResourceData, meta interface{})
serverName := id.Path["servers"]
name := id.Path["databases"]

resp, err := client.Get(resGroup, serverName, name)
resp, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] PostgreSQL Database '%s' was not found (resource group '%s')", name, resGroup)
Expand All @@ -119,6 +126,7 @@ func resourceArmPostgreSQLDatabaseRead(d *schema.ResourceData, meta interface{})

func resourceArmPostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlDatabasesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -128,8 +136,21 @@ func resourceArmPostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{
serverName := id.Path["servers"]
name := id.Path["databases"]

_, error := client.Delete(resGroup, serverName, name, make(chan struct{}))
err = <-error
future, err := client.Delete(ctx, resGroup, serverName, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

return err
return nil
}
6 changes: 4 additions & 2 deletions azurerm/resource_arm_postgresql_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func testCheckAzureRMPostgreSQLDatabaseExists(name string) resource.TestCheckFun
}

client := testAccProvider.Meta().(*ArmClient).postgresqlDatabasesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.Get(resourceGroup, serverName, name)
resp, err := client.Get(ctx, resourceGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: PostgreSQL Database %q (server %q resource group: %q) does not exist", name, serverName, resourceGroup)
Expand All @@ -64,6 +65,7 @@ func testCheckAzureRMPostgreSQLDatabaseExists(name string) resource.TestCheckFun

func testCheckAzureRMPostgreSQLDatabaseDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).postgresqlDatabasesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_postgresql_database" {
Expand All @@ -74,7 +76,7 @@ func testCheckAzureRMPostgreSQLDatabaseDestroy(s *terraform.State) error {
serverName := rs.Primary.Attributes["server_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := client.Get(resourceGroup, serverName, name)
resp, err := client.Get(ctx, resourceGroup, serverName, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
Expand Down
37 changes: 29 additions & 8 deletions azurerm/resource_arm_postgresql_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/arm/postgresql"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -50,6 +51,7 @@ func resourceArmPostgreSQLFirewallRule() *schema.Resource {

func resourceArmPostgreSQLFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM PostgreSQL Firewall Rule creation.")

Expand All @@ -66,13 +68,17 @@ func resourceArmPostgreSQLFirewallRuleCreate(d *schema.ResourceData, meta interf
},
}

_, error := client.CreateOrUpdate(resGroup, serverName, name, properties, make(chan struct{}))
err := <-error
future, err := client.CreateOrUpdate(ctx, resGroup, serverName, name, properties)
if err != nil {
return err
}

read, err := client.Get(resGroup, serverName, name)
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return err
}

read, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
return err
}
Expand All @@ -87,6 +93,7 @@ func resourceArmPostgreSQLFirewallRuleCreate(d *schema.ResourceData, meta interf

func resourceArmPostgreSQLFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -96,7 +103,7 @@ func resourceArmPostgreSQLFirewallRuleRead(d *schema.ResourceData, meta interfac
serverName := id.Path["servers"]
name := id.Path["firewallRules"]

resp, err := client.Get(resGroup, serverName, name)
resp, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] PostgreSQL Firewall Rule '%s' was not found (resource group '%s')", name, resGroup)
Expand All @@ -118,6 +125,7 @@ func resourceArmPostgreSQLFirewallRuleRead(d *schema.ResourceData, meta interfac

func resourceArmPostgreSQLFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).postgresqlFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -127,8 +135,21 @@ func resourceArmPostgreSQLFirewallRuleDelete(d *schema.ResourceData, meta interf
serverName := id.Path["servers"]
name := id.Path["firewallRules"]

_, error := client.Delete(resGroup, serverName, name, make(chan struct{}))
err = <-error
future, err := client.Delete(ctx, resGroup, serverName, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
}

return err
return nil
}
Loading

0 comments on commit 103811f

Please sign in to comment.