Skip to content

Commit

Permalink
azurerm_spring_cloud_service supports service_registry_enabled (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu authored Apr 21, 2022
1 parent 6d7eafd commit 0bb6faa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/services/springcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Client struct {
MonitoringSettingsClient *appplatform.MonitoringSettingsClient
DeploymentsClient *appplatform.DeploymentsClient
ServicesClient *appplatform.ServicesClient
ServiceRegistryClient *appplatform.ServiceRegistriesClient
StoragesClient *appplatform.StoragesClient
}

Expand Down Expand Up @@ -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)

Expand All @@ -54,6 +58,7 @@ func NewClient(o *common.ClientOptions) *Client {
DeploymentsClient: &deploymentsClient,
MonitoringSettingsClient: &monitoringSettingsClient,
ServicesClient: &servicesClient,
ServiceRegistryClient: &serviceRegistryClient,
StoragesClient: &storageClient,
}
}
55 changes: 55 additions & 0 deletions internal/services/springcloud/spring_cloud_service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func resourceSpringCloudService() *pluginsdk.Resource {
},
},

"service_registry_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"outbound_public_ip_addresses": {
Type: pluginsdk.TypeList,
Computed: true,
Expand Down Expand Up @@ -274,6 +279,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()
Expand Down Expand Up @@ -339,13 +345,25 @@ 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)
}

func resourceSpringCloudServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
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()

Expand Down Expand Up @@ -400,13 +418,36 @@ 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)
}

func resourceSpringCloudServiceRead(d *pluginsdk.ResourceData, meta interface{}) error {
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()

Expand Down Expand Up @@ -435,13 +476,27 @@ 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))
if resp.Sku != nil {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/spring_cloud_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0bb6faa

Please sign in to comment.