Skip to content

Commit

Permalink
fix location on schematics resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 committed Sep 13, 2022
1 parent a4e4184 commit 9faf85f
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 90 deletions.
6 changes: 5 additions & 1 deletion ibm/conns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,11 @@ func (session clientSession) ContainerRegistryV1() (*containerregistryv1.Contain

// SchematicsAPI provides schematics Service APIs ...
func (sess clientSession) SchematicsV1() (*schematicsv1.SchematicsV1, error) {
return sess.schematicsClient, sess.schematicsClientErr
if sess.schematicsClientErr != nil {
return sess.schematicsClient, sess.schematicsClientErr
}
return sess.schematicsClient.Clone(), nil
// return sess.schematicsClient, sess.schematicsClientErr
}

// FunctionClient ...
Expand Down
14 changes: 6 additions & 8 deletions ibm/service/schematics/data_source_ibm_schematics_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func DataSourceIBMSchematicsAction() *schema.Resource {
Required: true,
Description: "Action Id. Use GET /actions API to look up the Action Ids in your IBM Cloud account.",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -43,6 +38,7 @@ func DataSourceIBMSchematicsAction() *schema.Resource {
},
"location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "List of locations supported by IBM Cloud Schematics service. While creating your workspace or action, choose the right region, since it cannot be changed. Note, this does not limit the location of the IBM Cloud resources, provisioned using Schematics.",
},
Expand Down Expand Up @@ -953,10 +949,12 @@ func dataSourceIBMSchematicsActionRead(context context.Context, d *schema.Resour
if err != nil {
return diag.FromErr(err)
}
if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}
getActionOptions := &schematicsv1.GetActionOptions{}

Expand Down
14 changes: 6 additions & 8 deletions ibm/service/schematics/data_source_ibm_schematics_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func DataSourceIBMSchematicsInventory() *schema.Resource {
Required: true,
Description: "Resource Inventory Id. Use `GET /v2/inventories` API to look up the Resource Inventory definition Ids in your IBM Cloud account.",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -48,6 +43,7 @@ func DataSourceIBMSchematicsInventory() *schema.Resource {
},
"location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "List of locations supported by IBM Cloud Schematics service. While creating your workspace or action, choose the right region, since it cannot be changed. Note, this does not limit the location of the IBM Cloud resources, provisioned using Schematics.",
},
Expand Down Expand Up @@ -98,10 +94,12 @@ func dataSourceIBMSchematicsInventoryRead(context context.Context, d *schema.Res
if err != nil {
return diag.FromErr(err)
}
if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}

getInventoryOptions := &schematicsv1.GetInventoryOptions{}
Expand Down
14 changes: 6 additions & 8 deletions ibm/service/schematics/data_source_ibm_schematics_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func DataSourceIBMSchematicsJob() *schema.Resource {
Required: true,
Description: "Job Id. Use `GET /v2/jobs` API to look up the Job Ids in your IBM Cloud account.",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
},
"command_object": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -322,6 +317,7 @@ func DataSourceIBMSchematicsJob() *schema.Resource {
},
"location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "List of locations supported by IBM Cloud Schematics service. While creating your workspace or action, choose the right region, since it cannot be changed. Note, this does not limit the location of the IBM Cloud resources, provisioned using Schematics.",
},
Expand Down Expand Up @@ -2822,10 +2818,12 @@ func dataSourceIBMSchematicsJobRead(context context.Context, d *schema.ResourceD
if err != nil {
return diag.FromErr(err)
}
if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}
getJobOptions := &schematicsv1.GetJobOptions{}

Expand Down
10 changes: 6 additions & 4 deletions ibm/service/schematics/data_source_ibm_schematics_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DataSourceIBMSchematicsOutput() *schema.Resource {
Required: true,
Description: "The ID of the workspace for which you want to retrieve output values. To find the workspace ID, use the `GET /workspaces` API.",
},
"region": {
"location": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
Expand Down Expand Up @@ -62,10 +62,12 @@ func dataSourceIBMSchematicsOutputRead(d *schema.ResourceData, meta interface{})
workspaceID := d.Get("workspace_id").(string)
templateID := d.Get("template_id").(string)

if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}

getWorkspaceOutputsOptions := &schematicsv1.GetWorkspaceOutputsOptions{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func DataSourceIBMSchematicsResourceQuery() *schema.Resource {
Required: true,
Description: "Resource query Id. Use `GET /v2/resource_query` API to look up the Resource query definition Ids in your IBM Cloud account.",
},
"region": {
"location": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
Expand Down Expand Up @@ -120,10 +120,12 @@ func dataSourceIBMSchematicsResourceQueryRead(context context.Context, d *schema
return diag.FromErr(err)
}

if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}
getResourcesQueryOptions := &schematicsv1.GetResourcesQueryOptions{}

Expand Down
10 changes: 6 additions & 4 deletions ibm/service/schematics/data_source_ibm_schematics_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DataSourceIBMSchematicsState() *schema.Resource {
Required: true,
Description: "The ID of the workspace for which you want to retrieve the Terraform statefile URL. To find the workspace ID, use the GET /v1/workspaces API.",
},
"region": {
"location": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
Expand Down Expand Up @@ -61,10 +61,12 @@ func dataSourceIBMSchematicsStateRead(context context.Context, d *schema.Resourc
if err != nil {
return diag.FromErr(err)
}
if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}

getWorkspaceTemplateStateOptions := &schematicsv1.GetWorkspaceTemplateStateOptions{}
Expand Down
39 changes: 20 additions & 19 deletions ibm/service/schematics/data_source_ibm_schematics_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func DataSourceIBMSchematicsWorkspace() *schema.Resource {
Required: true,
Description: "The ID of the workspace. To find the workspace ID, use the `GET /v1/workspaces` API.",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The Region of the workspace.",
},
"applied_shareddata_ids": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -120,6 +115,7 @@ func DataSourceIBMSchematicsWorkspace() *schema.Resource {
},
"location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The IBM Cloud location where your workspace was provisioned.",
},
Expand Down Expand Up @@ -560,31 +556,36 @@ func DataSourceIBMSchematicsWorkspace() *schema.Resource {
}
}

func SchematicsEndpointURL(region, meta interface{}) (string, error) {
func SchematicsEndpointURL(region, meta interface{}) (string, bool, error) {
sess, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return "", err
}
// overide provider region with resource/datasource region argument
// update client with updated region endpoint
schematicsEndpoint := fmt.Sprintf("https://%s.%s", fmt.Sprintf("%s.schematics", region), "cloud.ibm.com")
visibility := sess.Config.Visibility
if visibility == "private" || visibility == "public-and-private" {
schematicsEndpoint = fmt.Sprintf("https://%s.%s", fmt.Sprintf("private-%s.schematics", region), "cloud.ibm.com")
return "", false, err
}
if region != sess.Config.Region {
// overide provider region with resource/datasource region argument
// update client with updated region endpoint
schematicsEndpoint := fmt.Sprintf("https://%s.%s", fmt.Sprintf("%s.schematics", region), "cloud.ibm.com")
visibility := sess.Config.Visibility
if visibility == "private" || visibility == "public-and-private" {
schematicsEndpoint = fmt.Sprintf("https://%s.%s", fmt.Sprintf("private-%s.schematics", region), "cloud.ibm.com")
}
schematicsEndpointURL := conns.EnvFallBack([]string{"IBMCLOUD_SCHEMATICS_API_ENDPOINT"}, schematicsEndpoint)
return schematicsEndpointURL, true, nil
}
schematicsEndpointURL := conns.EnvFallBack([]string{"IBMCLOUD_SCHEMATICS_API_ENDPOINT"}, schematicsEndpoint)
return schematicsEndpointURL, nil
return "", false, nil
}

func dataSourceIBMSchematicsWorkspaceRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schematicsClient, err := meta.(conns.ClientSession).SchematicsV1()
if err != nil {
return diag.FromErr(err)
}
if r, ok := d.GetOk("region"); ok {
if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, _ := SchematicsEndpointURL(region, meta)
schematicsClient.Service.Options.URL = schematicsURL
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}

getWorkspaceOptions := &schematicsv1.GetWorkspaceOptions{}
Expand Down
31 changes: 27 additions & 4 deletions ibm/service/schematics/resource_ibm_schematics_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"strings"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
Expand Down Expand Up @@ -47,6 +48,7 @@ func ResourceIBMSchematicsAction() *schema.Resource {
"location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.InvokeValidator("ibm_schematics_action", "location"),
Description: "List of locations supported by IBM Cloud Schematics service. While creating your workspace or action, choose the right region, since it cannot be changed. Note, this does not limit the location of the IBM Cloud resources, provisioned using Schematics.",
},
Expand Down Expand Up @@ -979,7 +981,13 @@ func resourceIBMSchematicsActionCreate(context context.Context, d *schema.Resour
if err != nil {
return diag.FromErr(err)
}

if r, ok := d.GetOk("location"); ok {
region := r.(string)
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
}
createActionOptions := &schematicsv1.CreateActionOptions{}

if _, ok := d.GetOk("name"); ok {
Expand Down Expand Up @@ -1337,7 +1345,12 @@ func resourceIBMSchematicsActionRead(context context.Context, d *schema.Resource
if err != nil {
return diag.FromErr(err)
}

actionIDSplit := strings.Split(d.Id(), ".")
region := actionIDSplit[0]
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
getActionOptions := &schematicsv1.GetActionOptions{}

getActionOptions.SetActionID(d.Id())
Expand All @@ -1351,7 +1364,6 @@ func resourceIBMSchematicsActionRead(context context.Context, d *schema.Resource
log.Printf("[DEBUG] GetActionWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetActionWithContext failed %s\n%s", err, response))
}

if err = d.Set("name", action.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
Expand Down Expand Up @@ -1781,6 +1793,12 @@ func resourceIBMSchematicsActionUpdate(context context.Context, d *schema.Resour
if err != nil {
return diag.FromErr(err)
}
actionIDSplit := strings.Split(d.Id(), ".")
region := actionIDSplit[0]
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}

updateActionOptions := &schematicsv1.UpdateActionOptions{}

Expand Down Expand Up @@ -1928,7 +1946,12 @@ func resourceIBMSchematicsActionDelete(context context.Context, d *schema.Resour
if err != nil {
return diag.FromErr(err)
}

actionIDSplit := strings.Split(d.Id(), ".")
region := actionIDSplit[0]
schematicsURL, updatedURL, _ := SchematicsEndpointURL(region, meta)
if updatedURL {
schematicsClient.Service.Options.URL = schematicsURL
}
deleteActionOptions := &schematicsv1.DeleteActionOptions{}

deleteActionOptions.SetActionID(d.Id())
Expand Down
Loading

0 comments on commit 9faf85f

Please sign in to comment.