diff --git a/azurerm/internal/services/springcloud/registration.go b/azurerm/internal/services/springcloud/registration.go index e5a02f9c637d..38f1264eb922 100644 --- a/azurerm/internal/services/springcloud/registration.go +++ b/azurerm/internal/services/springcloud/registration.go @@ -21,6 +21,7 @@ func (r Registration) WebsiteCategories() []string { // SupportedDataSources returns the supported Data Sources supported by this Service func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ + "azurerm_spring_cloud_app": dataSourceSpringCloudApp(), "azurerm_spring_cloud_service": dataSourceSpringCloudService(), } } diff --git a/azurerm/internal/services/springcloud/spring_cloud_app_data_source.go b/azurerm/internal/services/springcloud/spring_cloud_app_data_source.go new file mode 100644 index 000000000000..8b0dfc59ea8a --- /dev/null +++ b/azurerm/internal/services/springcloud/spring_cloud_app_data_source.go @@ -0,0 +1,140 @@ +package springcloud + +import ( + "fmt" + "time" + + "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/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/springcloud/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/springcloud/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceSpringCloudApp() *schema.Resource { + return &schema.Resource{ + Read: dataSourceSpringCloudAppRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.SpringCloudAppName, + }, + + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "service_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.SpringCloudServiceName, + }, + + "fqdn": { + Type: schema.TypeString, + Computed: true, + }, + + "https_only": { + Type: schema.TypeBool, + Computed: true, + }, + + "identity": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Computed: true, + }, + + "principal_id": { + Type: schema.TypeString, + Computed: true, + }, + + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "is_public": { + Type: schema.TypeBool, + Computed: true, + }, + + "persistent_disk": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "mount_path": { + Type: schema.TypeString, + Computed: true, + }, + + "size_in_gb": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + + "url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceSpringCloudAppRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).AppPlatform.AppsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id := parse.NewSpringCloudAppID(subscriptionId, d.Get("resource_group_name").(string), d.Get("service_name").(string), d.Get("name").(string)) + + resp, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.AppName, "") + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("%s was not found", id) + } + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + d.SetId(id.ID()) + + d.Set("name", id.AppName) + d.Set("service_name", id.SpringName) + d.Set("resource_group_name", id.ResourceGroup) + if err := d.Set("identity", flattenSpringCloudAppIdentity(resp.Identity)); err != nil { + return fmt.Errorf("setting `identity`: %s", err) + } + + if prop := resp.Properties; prop != nil { + d.Set("fqdn", prop.Fqdn) + d.Set("https_only", prop.HTTPSOnly) + d.Set("is_public", prop.Public) + d.Set("url", prop.URL) + + if err := d.Set("persistent_disk", flattenSpringCloudAppPersistentDisk(prop.PersistentDisk)); err != nil { + return fmt.Errorf("setting `persistent_disk`: %s", err) + } + } + + return nil +} diff --git a/azurerm/internal/services/springcloud/spring_cloud_app_data_source_test.go b/azurerm/internal/services/springcloud/spring_cloud_app_data_source_test.go new file mode 100644 index 000000000000..0ebc8fffa819 --- /dev/null +++ b/azurerm/internal/services/springcloud/spring_cloud_app_data_source_test.go @@ -0,0 +1,39 @@ +package springcloud_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check" +) + +type SpringCloudAppDataSource struct { +} + +func TestAccDataSourceSpringCloudApp_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_spring_cloud_app", "test") + r := SpringCloudAppDataSource{} + + data.DataSourceTest(t, []resource.TestStep{ + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("id").Exists(), + ), + }, + }) +} + +func (SpringCloudAppDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_spring_cloud_app" "test" { + name = azurerm_spring_cloud_app.test.name + resource_group_name = azurerm_spring_cloud_app.test.resource_group_name + service_name = azurerm_spring_cloud_app.test.service_name +} +`, SpringCloudAppResource{}.basic(data)) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 74934af68b35..df917fb61d09 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -618,6 +618,10 @@ azurerm_snapshot +