diff --git a/internal/services/springcloud/client/client.go b/internal/services/springcloud/client/client.go index 1ce8779d918c..e7245a728c55 100644 --- a/internal/services/springcloud/client/client.go +++ b/internal/services/springcloud/client/client.go @@ -14,6 +14,7 @@ type Client struct { MonitoringSettingsClient *appplatform.MonitoringSettingsClient DeploymentsClient *appplatform.DeploymentsClient ServicesClient *appplatform.ServicesClient + ServiceRegistryClient *appplatform.ServiceRegistriesClient StoragesClient *appplatform.StoragesClient } @@ -42,6 +43,9 @@ func NewClient(o *common.ClientOptions) *Client { servicesClient := appplatform.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&servicesClient.Client, o.ResourceManagerAuthorizer) + serviceRegistryClient := appplatform.NewServiceRegistriesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&serviceRegistryClient.Client, o.ResourceManagerAuthorizer) + storageClient := appplatform.NewStoragesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&storageClient.Client, o.ResourceManagerAuthorizer) @@ -54,6 +58,7 @@ func NewClient(o *common.ClientOptions) *Client { DeploymentsClient: &deploymentsClient, MonitoringSettingsClient: &monitoringSettingsClient, ServicesClient: &servicesClient, + ServiceRegistryClient: &serviceRegistryClient, StoragesClient: &storageClient, } } diff --git a/internal/services/springcloud/spring_cloud_service_resource.go b/internal/services/springcloud/spring_cloud_service_resource.go index a3c2d849c98f..a95c6fd89b3a 100644 --- a/internal/services/springcloud/spring_cloud_service_resource.go +++ b/internal/services/springcloud/spring_cloud_service_resource.go @@ -219,6 +219,11 @@ func resourceSpringCloudService() *pluginsdk.Resource { }, }, + "service_registry_enabled": { + Type: pluginsdk.TypeBool, + Optional: true, + }, + "outbound_public_ip_addresses": { Type: pluginsdk.TypeList, Computed: true, @@ -275,6 +280,7 @@ func resourceSpringCloudServiceCreate(d *pluginsdk.ResourceData, meta interface{ client := meta.(*clients.Client).AppPlatform.ServicesClient configServersClient := meta.(*clients.Client).AppPlatform.ConfigServersClient monitoringSettingsClient := meta.(*clients.Client).AppPlatform.MonitoringSettingsClient + serviceRegistryClient := meta.(*clients.Client).AppPlatform.ServiceRegistryClient subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() @@ -340,6 +346,17 @@ func resourceSpringCloudServiceCreate(d *pluginsdk.ResourceData, meta interface{ } log.Printf("[DEBUG] Updated Monitor Settings for %s.", id) + if d.Get("service_registry_enabled").(bool) { + future, err := serviceRegistryClient.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, "default") + if err != nil { + return fmt.Errorf("creating service registry %s: %+v", id, err) + } + + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("waiting for creation service registry of %s: %+v", id, err) + } + } + return resourceSpringCloudServiceRead(d, meta) } @@ -347,6 +364,7 @@ func resourceSpringCloudServiceUpdate(d *pluginsdk.ResourceData, meta interface{ client := meta.(*clients.Client).AppPlatform.ServicesClient configServersClient := meta.(*clients.Client).AppPlatform.ConfigServersClient monitoringSettingsClient := meta.(*clients.Client).AppPlatform.MonitoringSettingsClient + serviceRegistryClient := meta.(*clients.Client).AppPlatform.ServiceRegistryClient ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() @@ -401,6 +419,28 @@ func resourceSpringCloudServiceUpdate(d *pluginsdk.ResourceData, meta interface{ log.Printf("[DEBUG] Updated Monitor Settings for %s.", id) } + if d.HasChange("service_registry_enabled") { + if d.Get("service_registry_enabled").(bool) { + future, err := serviceRegistryClient.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, "default") + if err != nil { + return fmt.Errorf("creating service registry of %s: %+v", id, err) + } + + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("waiting for creation service registry of %s: %+v", id, err) + } + } else { + future, err := serviceRegistryClient.Delete(ctx, id.ResourceGroup, id.SpringName, "default") + if err != nil { + return fmt.Errorf("deleting service registry of %s: %+v", id, err) + } + + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("waiting for deletion service registry of %s: %+v", id, err) + } + } + } + return resourceSpringCloudServiceRead(d, meta) } @@ -408,6 +448,7 @@ func resourceSpringCloudServiceRead(d *pluginsdk.ResourceData, meta interface{}) client := meta.(*clients.Client).AppPlatform.ServicesClient configServersClient := meta.(*clients.Client).AppPlatform.ConfigServersClient monitoringSettingsClient := meta.(*clients.Client).AppPlatform.MonitoringSettingsClient + serviceRegistryClient := meta.(*clients.Client).AppPlatform.ServiceRegistryClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -436,6 +477,18 @@ func resourceSpringCloudServiceRead(d *pluginsdk.ResourceData, meta interface{}) return fmt.Errorf("retrieving monitoring settings for %s: %+v", id, err) } + serviceRegistryEnabled := true + serviceRegistry, err := serviceRegistryClient.Get(ctx, id.ResourceGroup, id.SpringName, "default") + if err != nil { + if !utils.ResponseWasNotFound(serviceRegistry.Response) { + return fmt.Errorf("retrieving service registry of %s: %+v", id, err) + } + serviceRegistryEnabled = false + } + if utils.ResponseWasNotFound(serviceRegistry.Response) { + serviceRegistryEnabled = false + } + d.Set("name", id.SpringName) d.Set("resource_group_name", id.ResourceGroup) d.Set("location", location.NormalizeNilable(resp.Location)) @@ -443,6 +496,8 @@ func resourceSpringCloudServiceRead(d *pluginsdk.ResourceData, meta interface{}) d.Set("sku_name", resp.Sku.Name) } + d.Set("service_registry_enabled", serviceRegistryEnabled) + if err := d.Set("config_server_git_setting", flattenSpringCloudConfigServerGitProperty(configServer.Properties, d)); err != nil { return fmt.Errorf("setting `config_server_git_setting`: %+v", err) } diff --git a/internal/services/springcloud/spring_cloud_service_resource_test.go b/internal/services/springcloud/spring_cloud_service_resource_test.go index 6834c1b393de..97563b9f4e5c 100644 --- a/internal/services/springcloud/spring_cloud_service_resource_test.go +++ b/internal/services/springcloud/spring_cloud_service_resource_test.go @@ -144,6 +144,35 @@ func TestAccSpringCloudService_requiresImport(t *testing.T) { }) } +func TestAccSpringCloudService_serviceRegistry(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_spring_cloud_service", "test") + r := SpringCloudServiceResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.serviceRegistry(data, false), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.serviceRegistry(data, true), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.serviceRegistry(data, false), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (t SpringCloudServiceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := parse.SpringCloudServiceID(state.ID) if err != nil { @@ -177,6 +206,27 @@ resource "azurerm_spring_cloud_service" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger) } +func (SpringCloudServiceResource) serviceRegistry(data acceptance.TestData, enabled bool) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-spring-%d" + location = "%s" +} + +resource "azurerm_spring_cloud_service" "test" { + name = "acctest-sc-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku_name = "E0" + service_registry_enabled = %t +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, enabled) +} + func (SpringCloudServiceResource) singleGitRepo(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { diff --git a/website/docs/r/spring_cloud_service.html.markdown b/website/docs/r/spring_cloud_service.html.markdown index 6f2ec438ebf9..d9811ed2fd25 100644 --- a/website/docs/r/spring_cloud_service.html.markdown +++ b/website/docs/r/spring_cloud_service.html.markdown @@ -70,6 +70,8 @@ The following arguments are supported: * `config_server_git_setting` - (Optional) A `config_server_git_setting` block as defined below. +* `service_registry_enabled` - (Optional) Whether enable the default Service Registry. + * `trace` - (Optional) A `trace` block as defined below. * `tags` - (Optional) A mapping of tags to assign to the resource.