-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement resource and data_source of Azure Spring Cloud
- Loading branch information
Showing
12 changed files
with
1,355 additions
and
0 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
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,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 | ||
} |
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,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) | ||
} |
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,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, | ||
} | ||
} |
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,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 | ||
} |
Oops, something went wrong.