Skip to content

Commit

Permalink
add new resource and data source for spring cloud and spring cloud co…
Browse files Browse the repository at this point in the history
…nfig server
  • Loading branch information
njuCZ committed Dec 13, 2019
1 parent 4f9071f commit 5dff6f9
Show file tree
Hide file tree
Showing 32 changed files with 6,891 additions and 0 deletions.
74 changes: 74 additions & 0 deletions azurerm/data_source_spring_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package azurerm

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
azappplatform "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmSpringCloud() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSpringCloudRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azappplatform.ValidateSpringCloudName,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"service_id": {
Type: schema.TypeString,
Computed: true,
},

"version": {
Type: schema.TypeInt,
Computed: true,
},

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmSpringCloudRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).AppPlatform.ServicesClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Spring Cloud %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading Spring Cloud %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if clusterResourceProperties := resp.Properties; clusterResourceProperties != nil {
d.Set("service_id", clusterResourceProperties.ServiceID)
d.Set("version", int(*clusterResourceProperties.Version))
}

return nil
}
168 changes: 168 additions & 0 deletions azurerm/data_source_spring_cloud_config_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package azurerm

import (
"fmt"

"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/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmSpringCloudConfigServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSpringCloudConfigServerRead,

Schema: map[string]*schema.Schema{
"spring_cloud_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"uri": {
Type: schema.TypeString,
Computed: true,
},
"host_key": {
Type: schema.TypeString,
Computed: true,
},
"host_key_algorithm": {
Type: schema.TypeString,
Computed: true,
},
"label": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"private_key": {
Type: schema.TypeString,
Computed: true,
},
"repositories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
"host_key": {
Type: schema.TypeString,
Computed: true,
},
"host_key_algorithm": {
Type: schema.TypeString,
Computed: true,
},
"label": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"pattern": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"private_key": {
Type: schema.TypeString,
Computed: true,
},
"search_paths": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"strict_host_key_checking": {
Type: schema.TypeBool,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"search_paths": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"strict_host_key_checking": {
Type: schema.TypeBool,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmSpringCloudConfigServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).AppPlatform.ServicesClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

springCloudId := d.Get("spring_cloud_id").(string)
id, err := azure.ParseAzureResourceID(springCloudId)
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
springCloudName := id.Path["Spring"]

resp, err := client.Get(ctx, resourceGroup, springCloudName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Spring Cloud %q (Resource Group %q) was not found", springCloudName, resourceGroup)
}
return fmt.Errorf("Error reading Spring Cloud %q (Resource Group %q): %+v", springCloudName, resourceGroup, err)
}

d.SetId(springCloudId)

if resp.Properties != nil && resp.Properties.ConfigServerProperties != nil && resp.Properties.ConfigServerProperties.ConfigServer != nil {
if props := resp.Properties.ConfigServerProperties.ConfigServer.GitProperty; props != nil {
d.Set("host_key", props.HostKey)
d.Set("host_key_algorithm", props.HostKeyAlgorithm)
d.Set("label", props.Label)
d.Set("password", props.Password)
d.Set("private_key", props.PrivateKey)
d.Set("strict_host_key_checking", props.StrictHostKeyChecking)
d.Set("uri", props.URI)
d.Set("username", props.Username)
d.Set("search_paths", props.SearchPaths)
if err := d.Set("repositories", flattenArmSpringCloudGitPatternRepository(props.Repositories)); err != nil {
return fmt.Errorf("Error setting `repositories`: %+v", err)
}
}
}

return nil
}
57 changes: 57 additions & 0 deletions azurerm/data_source_spring_cloud_config_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMSpringCloudConfigServer_complete(t *testing.T) {
dataSourceName := "data.azurerm_spring_cloud_config_server.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceSpringCloudConfigServer_complete(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "uri", "https://github.com/Azure-Samples/piggymetrics"),
resource.TestCheckResourceAttr(dataSourceName, "label", "config"),
resource.TestCheckResourceAttr(dataSourceName, "search_paths.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "search_paths.0", "dir1"),
resource.TestCheckResourceAttr(dataSourceName, "search_paths.1", "dir2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.name", "repo1"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.uri", "https://github.com/Azure-Samples/piggymetrics"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.label", "config"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.search_paths.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.search_paths.0", "dir1"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.0.search_paths.1", "dir2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.name", "repo2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.uri", "https://github.com/Azure-Samples/piggymetrics"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.label", "config"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.search_paths.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.search_paths.0", "dir1"),
resource.TestCheckResourceAttr(dataSourceName, "repositories.1.search_paths.1", "dir2"),
),
},
},
})
}

func testAccDataSourceSpringCloudConfigServer_complete(rInt int, location string) string {
config := testAccAzureRMSpringCloudConfigServer_complete(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_spring_cloud_config_server" "test" {
spring_cloud_id = azurerm_spring_cloud.test.id
}
`, config)
}
45 changes: 45 additions & 0 deletions azurerm/data_source_spring_cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMSpringCloud_complete(t *testing.T) {
dataSourceName := "data.azurerm_spring_cloud.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceSpringCloud_complete(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttrSet(dataSourceName, "service_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "version"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(dataSourceName, "tags.env", "test"),
resource.TestCheckResourceAttr(dataSourceName, "tags.version", "1"),
),
},
},
})
}

func testAccDataSourceSpringCloud_complete(rInt int, location string) string {
config := testAccAzureRMSpringCloud_complete(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_spring_cloud" "test" {
name = azurerm_spring_cloud.test.name
resource_group_name = azurerm_spring_cloud.test.resource_group_name
}
`, config)
}
3 changes: 3 additions & 0 deletions azurerm/internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
apiManagement "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/client"
appConfiguration "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appconfiguration/client"
applicationInsights "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/applicationinsights/client"
appPlatform "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform/client"
authorization "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/authorization/client"
automation "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/automation/client"
batch "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/batch/client"
Expand Down Expand Up @@ -77,6 +78,7 @@ type Client struct {
ApiManagement *apiManagement.Client
AppConfiguration *appConfiguration.Client
AppInsights *applicationInsights.Client
AppPlatform *appPlatform.Client
Authorization *authorization.Client
Automation *automation.Client
Batch *batch.Client
Expand Down Expand Up @@ -141,6 +143,7 @@ func (client *Client) Build(o *common.ClientOptions) error {
client.ApiManagement = apiManagement.NewClient(o)
client.AppConfiguration = appConfiguration.NewClient(o)
client.AppInsights = applicationInsights.NewClient(o)
client.AppPlatform = appPlatform.NewClient(o)
client.Authorization = authorization.NewClient(o)
client.Automation = automation.NewClient(o)
client.Batch = batch.NewClient(o)
Expand Down
19 changes: 19 additions & 0 deletions azurerm/internal/services/appplatform/client/client.go
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/preview/appplatform/mgmt/2019-05-01-preview/appplatform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
ServicesClient *appplatform.ServicesClient
}

func NewClient(o *common.ClientOptions) *Client {
ServicesClient := appplatform.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ServicesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ServicesClient: &ServicesClient,
}
}
Loading

0 comments on commit 5dff6f9

Please sign in to comment.