Skip to content

Commit

Permalink
implement resource and data_source of Azure Spring Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed Nov 20, 2019
1 parent 21350b4 commit 7e8bdfd
Show file tree
Hide file tree
Showing 12 changed files with 1,355 additions and 0 deletions.
15 changes: 15 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/analysisservices"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/applicationinsights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/appplatform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/authorization"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/automation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/batch"
Expand Down Expand Up @@ -90,6 +91,19 @@ type ArmClient struct {

// Services
// NOTE: all new services should be Public as they're going to be relocated in the near-future
AnalysisServices *analysisservices.Client
ApiManagement *apimanagement.Client
AppInsights *applicationinsights.Client
AppPlatform *appplatform.Client
Automation *automation.Client
Authorization *authorization.Client
Batch *batch.Client
Bot *bot.Client
Cdn *cdn.Client
Cognitive *cognitive.Client
Compute *clients.ComputeClient
Containers *containers.Client
Cosmos *cosmos.Client
DataBricks *databricks.Client
DataFactory *datafactory.Client
Datalake *datalake.Client
Expand Down Expand Up @@ -218,6 +232,7 @@ func getArmClient(authConfig *authentication.Config, skipProviderRegistration bo
client.AnalysisServices = analysisservices.BuildClient(o)
client.ApiManagement = apimanagement.BuildClient(o)
client.AppInsights = applicationinsights.BuildClient(o)
client.AppPlatform = appplatform.BuildClient(o)
client.Automation = automation.BuildClient(o)
client.Authorization = authorization.BuildClient(o)
client.Batch = batch.BuildClient(o)
Expand Down
206 changes: 206 additions & 0 deletions azurerm/data_source_spring_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package azurerm

import (
"fmt"

"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": azure.SchemaResourceGroupNameForDataSource(),

"config_server": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"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,
},
"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,
},
"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,
},
},
},
},

"config_server_error": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
},
"message": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"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 := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group").(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", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if clusterResourceProperties := resp.Properties; clusterResourceProperties != nil {
if clusterResourceProperties.ConfigServerProperties != nil {
if err := d.Set("config_server_error", flattenArmSpringCloudError(clusterResourceProperties.ConfigServerProperties.Error)); err != nil {
return fmt.Errorf("Error setting `error`: %+v", err)
}

if clusterResourceProperties.ConfigServerProperties.ConfigServer != nil {
if err := d.Set("config_server", flattenArmSpringCloudConfigServerGitProperty(clusterResourceProperties.ConfigServerProperties.ConfigServer.GitProperty)); err != nil {
return fmt.Errorf("Error setting `config_server`: %+v", err)
}
}
}

d.Set("service_id", clusterResourceProperties.ServiceID)
d.Set("version", int(*clusterResourceProperties.Version))
}

return nil
}
49 changes: 49 additions & 0 deletions azurerm/data_source_spring_cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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: testAccDataSourcePrivateLinkService_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"),
resource.TestCheckResourceAttr(dataSourceName, "config_server_error.#", "0"),
resource.TestCheckResourceAttrSet(dataSourceName, "config_server"),
resource.TestCheckResourceAttr(dataSourceName, "config_server.uri", "https://github.com/Azure-Samples/piggymetrics"),
resource.TestCheckResourceAttr(dataSourceName, "config_server.label", "config"),
),
},
},
})
}

func testAccDataSourcePrivateLinkService_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 = azurerm_spring_cloud.test.resource_group
}
`, config)
}
19 changes: 19 additions & 0 deletions azurerm/internal/services/appplatform/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package appplatform

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 BuildClient(o *common.ClientOptions) *Client {
ServicesClient := appplatform.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ServicesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ServicesClient: &ServicesClient,
}
}
47 changes: 47 additions & 0 deletions azurerm/internal/services/appplatform/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package appplatform

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

func ValidateSpringCloudName(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
return nil, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

// The name attribute rules are :
// 1. can contain only lowercase letters, numbers and hyphens.
// 2. The first character must be a letter.
// 3. The last character must be a letter or number
// 3. The value must be between 4 and 32 characters long

if len(v) < 4 || len(v) > 32 {
errors = append(errors, fmt.Errorf("%s must be between 4 and 32 characters long", k))
} else {
if m, _ := validate.RegExHelper(i, k, `^([a-z])([a-z\d-]{0,30})([a-z\d])$`); !m {
errors = append(errors, fmt.Errorf("%s can contain only lowercase letters, numbers and hyphens, begin with a letter, end with a letter or number", k))
}
}

return nil, errors
}

func ValidateConfigServerURI(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
return nil, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

// the config server URI should be started with http://, https://, git@, or ssh://
if !strings.HasPrefix(v, "http://") &&
!strings.HasPrefix(v, "https://") &&
!strings.HasPrefix(v, "git@") &&
!strings.HasPrefix(v, "ssh://") {
errors = append(errors, fmt.Errorf("%s should be started with http://, https://, git@, or ssh://", k))
}
return nil, errors
}
Loading

0 comments on commit 7e8bdfd

Please sign in to comment.