Skip to content

Commit

Permalink
Healthcareapi/workspace (hashicorp#15759)
Browse files Browse the repository at this point in the history
* add pandora sdk for healthcare api

* add workspace

* workspace

* hc worksapce update

* remove pandora sdk

* fmt

* sort import

* update

* tidy vendor

* update workspace

* rename

* go mod

* add healthcare workspace data source docs and add an update test for tags for healthcare workspace resource

Co-authored-by: Steph <[email protected]>
  • Loading branch information
xiaxyi and stephybun authored Mar 16, 2022
1 parent acc2181 commit 7ac8f89
Show file tree
Hide file tree
Showing 38 changed files with 8,424 additions and 1,440 deletions.
13 changes: 9 additions & 4 deletions internal/services/healthcare/client/client.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package client

import (
healthcare "github.com/Azure/azure-sdk-for-go/services/healthcareapis/mgmt/2020-03-30/healthcareapis"
"github.com/Azure/azure-sdk-for-go/services/healthcareapis/mgmt/2021-11-01/healthcareapis"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
HealthcareServiceClient *healthcare.ServicesClient
HealthcareServiceClient *healthcareapis.ServicesClient
HealthcareWorkspaceClient *healthcareapis.WorkspacesClient
}

func NewClient(o *common.ClientOptions) *Client {
HealthcareServiceClient := healthcare.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
HealthcareServiceClient := healthcareapis.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&HealthcareServiceClient.Client, o.ResourceManagerAuthorizer)

HealthcareWorkspaceClient := healthcareapis.NewWorkspacesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&HealthcareWorkspaceClient.Client, o.ResourceManagerAuthorizer)

return &Client{
HealthcareServiceClient: &HealthcareServiceClient,
HealthcareServiceClient: &HealthcareServiceClient,
HealthcareWorkspaceClient: &HealthcareWorkspaceClient,
}
}
16 changes: 8 additions & 8 deletions internal/services/healthcare/healthcare_service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/healthcareapis/mgmt/2020-03-30/healthcareapis"
"github.com/Azure/azure-sdk-for-go/services/healthcareapis/mgmt/2021-11-01/healthcareapis"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -54,11 +54,11 @@ func resourceHealthcareService() *pluginsdk.Resource {
"kind": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(healthcareapis.Fhir),
Default: string(healthcareapis.KindFhir),
ValidateFunc: validation.StringInSlice([]string{
string(healthcareapis.Fhir),
string(healthcareapis.FhirR4),
string(healthcareapis.FhirStu3),
string(healthcareapis.KindFhir),
string(healthcareapis.KindFhirR4),
string(healthcareapis.KindFhirStu3),
}, false),
},

Expand Down Expand Up @@ -246,9 +246,9 @@ func resourceHealthcareServiceCreateUpdate(d *pluginsdk.ResourceData, meta inter

publicNetworkAccess := d.Get("public_network_access_enabled").(bool)
if !publicNetworkAccess {
healthcareServiceDescription.Properties.PublicNetworkAccess = healthcareapis.Disabled
healthcareServiceDescription.Properties.PublicNetworkAccess = healthcareapis.PublicNetworkAccessDisabled
} else {
healthcareServiceDescription.Properties.PublicNetworkAccess = healthcareapis.Enabled
healthcareServiceDescription.Properties.PublicNetworkAccess = healthcareapis.PublicNetworkAccessEnabled
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, healthcareServiceDescription)
Expand Down Expand Up @@ -311,7 +311,7 @@ func resourceHealthcareServiceRead(d *pluginsdk.ResourceData, meta interface{})
}
d.Set("cosmosdb_key_vault_key_versionless_id", cosmodDbKeyVaultKeyVersionlessId)
d.Set("cosmosdb_throughput", cosmosDbThroughput)
if props.PublicNetworkAccess == healthcareapis.Enabled {
if props.PublicNetworkAccess == healthcareapis.PublicNetworkAccessEnabled {
d.Set("public_network_access_enabled", true)
} else {
d.Set("public_network_access_enabled", false)
Expand Down
67 changes: 67 additions & 0 deletions internal/services/healthcare/healthcare_workspace_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package healthcare

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceHealthcareWorkspace() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceHealthcareWorkspaceRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.WorkspaceName,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.LocationComputed(),

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceHealthcareWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HealthCare.HealthcareWorkspaceClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewWorkspaceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
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.Name)
d.Set("resource_group_name", id.ResourceGroup)

if locations := resp.Location; locations != nil {
d.Set("location", location.Normalize(*locations))
}

return tags.FlattenAndSet(d, resp.Tags)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package healthcare_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type HealthCareWorkspaceDataSource struct{}

func TestAccHealthCareWorkspaceDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_healthcare_workspace", "test")
r := HealthCareWorkspaceDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists()),
},
})
}

func (HealthCareWorkspaceDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_healthcare_workspace" "test" {
name = azurerm_healthcare_workspace.test.name
resource_group_name = azurerm_healthcare_workspace.test.resource_group_name
}
`, HealthCareWorkspaceResource{}.complete(data))
}
217 changes: 217 additions & 0 deletions internal/services/healthcare/healthcare_workspace_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package healthcare

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/healthcareapis/mgmt/2021-11-01/healthcareapis"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/healthcare/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceHealthcareApisWorkspace() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceHealthcareApisWorkspaceCreate,
Read: resourceHealthcareApisWorkspaceRead,
Update: resourceHealthcareApisWorkspaceUpdate,
Delete: resourceHealthcareApisWorkspaceDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.WorkspaceID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.WorkspaceName,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"private_endpoint_connection": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"tags": commonschema.Tags(),
},
}
}

func resourceHealthcareApisWorkspaceCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HealthCare.HealthcareWorkspaceClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Healthcare Workspace creation.")

id := parse.NewWorkspaceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_healthcare_workspace", id.ID())
}
}

location := location.Normalize(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

parameters := healthcareapis.Workspace{
Location: &location,
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters)
if err != nil {
return fmt.Errorf("creating/ updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceHealthcareApisWorkspaceRead(d, meta)
}

func resourceHealthcareApisWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HealthCare.HealthcareWorkspaceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.WorkspaceID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if locations := resp.Location; locations != nil {
d.Set("location", location.Normalize(*locations))
}

if props := resp.Properties; props != nil {
d.Set("private_endpoint_connection", flattenWorkspacePrivateEndpoint(props.PrivateEndpointConnections))
}

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceHealthcareApisWorkspaceUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HealthCare.HealthcareWorkspaceClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.WorkspaceID(d.Id())
if err != nil {
return err
}

t := d.Get("tags").(map[string]interface{})

parameters := healthcareapis.WorkspacePatchResource{
Tags: tags.Expand(t),
}

future, err := client.Update(ctx, id.ResourceGroup, id.Name, parameters)
if err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for update of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceHealthcareApisWorkspaceRead(d, meta)
}

func resourceHealthcareApisWorkspaceDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HealthCare.HealthcareWorkspaceClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.WorkspaceID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the deletion of %s: %+v", *id, err)
}
return nil
}

func flattenWorkspacePrivateEndpoint(input *[]healthcareapis.PrivateEndpointConnection) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, endpoint := range *input {
result := map[string]interface{}{}
if endpoint.Name != nil {
result["name"] = *endpoint.Name
}

if endpoint.ID != nil {
result["id"] = *endpoint.ID
}
}
return results
}
Loading

0 comments on commit 7ac8f89

Please sign in to comment.