Skip to content

Commit

Permalink
Refactor Workspace data source and documentation (#5106)
Browse files Browse the repository at this point in the history
* Refactor workspace data source and documentation

* Pull ibm_ pi_constants.go from constant-refactor

* Update branch resolve conflicts
  • Loading branch information
Diptipowervs authored Feb 5, 2024
1 parent 4c67eda commit d727172
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 104 deletions.
52 changes: 23 additions & 29 deletions ibm/service/power/data_source_ibm_pi_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,70 @@ import (
"context"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const (
WorkspaceCreationDate = "creation_date"
WorkspaceCRN = "crn"
WorkspaceRegion = "region"
WorkspaceType = "type"
WorkspaceUrl = "url"
)

func DatasourceIBMPIWorkspace() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIWorkspaceRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_WorkspaceCapabilities: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace Capabilities",
Description: "Workspace Capabilities.",
Elem: &schema.Schema{
Type: schema.TypeBool,
},
Type: schema.TypeMap,
},
Attr_WorkspaceDetails: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace information",
Description: "Workspace information.",
Type: schema.TypeMap,
},
Attr_WorkspaceLocation: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace location",
Description: "Workspace location.",
Type: schema.TypeMap,
},
Attr_WorkspaceName: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace name",
Description: "Workspace name.",
Type: schema.TypeString,
},
Attr_WorkspaceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace status",
Description: "Workspace status, active, critical, failed, provisioning.",
Type: schema.TypeString,
},
Attr_WorkspaceType: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace type",
Description: "Workspace type, off-premises or on-premises.",
Type: schema.TypeString,
},
},
}
}

func dataSourceIBMPIWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// session
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
client := instance.NewIBMPIWorkspacesClient(ctx, sess, cloudInstanceID)
wsData, err := client.Get(cloudInstanceID)
if err != nil {
Expand All @@ -88,14 +82,14 @@ func dataSourceIBMPIWorkspaceRead(ctx context.Context, d *schema.ResourceData, m
d.Set(Attr_WorkspaceType, wsData.Type)
d.Set(Attr_WorkspaceCapabilities, wsData.Capabilities)
wsdetails := map[string]interface{}{
WorkspaceCreationDate: wsData.Details.CreationDate.String(),
WorkspaceCRN: *wsData.Details.Crn,
Attr_CreationDate: wsData.Details.CreationDate.String(),
Attr_CRN: *wsData.Details.Crn,
}
d.Set(Attr_WorkspaceDetails, flex.Flatten(wsdetails))
wslocation := map[string]interface{}{
WorkspaceRegion: *wsData.Location.Region,
WorkspaceType: wsData.Location.Type,
WorkspaceUrl: wsData.Location.URL,
Attr_Region: *wsData.Location.Region,
Attr_Type: wsData.Location.Type,
Attr_URL: wsData.Location.URL,
}
d.Set(Attr_WorkspaceLocation, flex.Flatten(wslocation))
d.SetId(*wsData.ID)
Expand Down
4 changes: 2 additions & 2 deletions ibm/service/power/data_source_ibm_pi_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestAccIBMPIWorkspaceDataSourceBasic(t *testing.T) {
},
})
}

func testAccCheckIBMPIWorkspaceDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_workspace" "test" {
pi_cloud_instance_id = "%s"
}
`, acc.Pi_cloud_instance_id)
}`, acc.Pi_cloud_instance_id)
}
65 changes: 31 additions & 34 deletions ibm/service/power/data_source_ibm_pi_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,68 @@ import (
"context"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const (
Workspaces = "workspaces"
)

func DatasourceIBMPIWorkspaces() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIWorkspacesRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Workspaces: {

// Attributes
Attr_Workspaces: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

Attr_WorkspaceCapabilities: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace Capabilities",
Description: "Workspace Capabilities.",
Elem: &schema.Schema{
Type: schema.TypeBool,
},
Type: schema.TypeMap,
},
Attr_WorkspaceDetails: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace information",
Description: "Workspace information.",
Type: schema.TypeMap,
},
Attr_WorkspaceID: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace ID",
Description: "Workspace ID.",
Type: schema.TypeString,
},
Attr_WorkspaceLocation: {
Type: schema.TypeMap,
Computed: true,
Description: "Workspace location",
Description: "Workspace location.",
Type: schema.TypeMap,
},
Attr_WorkspaceName: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace name",
Description: "Workspace name.",
Type: schema.TypeString,
},
Attr_WorkspaceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace status",
Description: "Workspace status, active, critical, failed, provisioning.",
Type: schema.TypeString,
},
Attr_WorkspaceType: {
Type: schema.TypeString,
Computed: true,
Description: "Workspace type",
Description: "Workspace type, off-premises or on-premises.",
Type: schema.TypeString,
},
},
},
Expand All @@ -79,13 +77,12 @@ func DatasourceIBMPIWorkspaces() *schema.Resource {
}
}
func dataSourceIBMPIWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// session
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
client := instance.NewIBMPIWorkspacesClient(ctx, sess, cloudInstanceID)
wsData, err := client.GetAll()
if err != nil {
Expand All @@ -95,26 +92,26 @@ func dataSourceIBMPIWorkspacesRead(ctx context.Context, d *schema.ResourceData,
for _, ws := range wsData.Workspaces {
if ws != nil {
workspace := map[string]interface{}{
Attr_WorkspaceName: ws.Name,
Attr_WorkspaceID: ws.ID,
Attr_WorkspaceStatus: ws.Status,
Attr_WorkspaceType: ws.Type,
Attr_WorkspaceCapabilities: ws.Capabilities,
Attr_WorkspaceDetails: map[string]interface{}{
WorkspaceCreationDate: ws.Details.CreationDate.String(),
WorkspaceCRN: *ws.Details.Crn,
Attr_CreationDate: ws.Details.CreationDate.String(),
Attr_CRN: *ws.Details.Crn,
},
Attr_WorkspaceID: ws.ID,
Attr_WorkspaceLocation: map[string]interface{}{
WorkspaceRegion: *ws.Location.Region,
WorkspaceType: ws.Location.Type,
WorkspaceUrl: ws.Location.URL,
Attr_Region: *ws.Location.Region,
Attr_Type: ws.Location.Type,
Attr_URL: ws.Location.URL,
},
Attr_WorkspaceName: ws.Name,
Attr_WorkspaceStatus: ws.Status,
Attr_WorkspaceType: ws.Type,
}
workspaces = append(workspaces, workspace)
}
}
var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
d.Set(Workspaces, workspaces)
d.Set(Attr_Workspaces, workspaces)
return nil
}
3 changes: 1 addition & 2 deletions ibm/service/power/data_source_ibm_pi_workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ func testAccCheckIBMPIWorkspacesDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_workspaces" "test" {
pi_cloud_instance_id = "%s"
}
`, acc.Pi_cloud_instance_id)
}`, acc.Pi_cloud_instance_id)
}
23 changes: 8 additions & 15 deletions website/docs/d/pi_workspace.html.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

subcategory: "Power Systems"
layout: "ibm"
page_title: "IBM: pi_workspace"
Expand All @@ -8,26 +7,22 @@ description: |-
---

# ibm_pi_workspace

Retrieve information about your Power Systems account workspace.

## Example usage

```terraform
data "ibm_pi_workspace" "workspace" {
pi_cloud_instance_id = "99fba9c9-66f9-99bc-9999-aca999ee9d9b"
}
```

## Notes

**Notes**
- Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
- If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
- `region` - `lon`
- `zone` - `lon04`

Example usage:

```terraform
provider "ibm" {
region = "lon"
Expand All @@ -36,13 +31,11 @@ Example usage:
```

## Argument reference

Review the argument references that you can specify for your data source.

- `pi_cloud_instance_id` - (Required, String) Cloud Instance ID of a PCloud Instance under your account.

## Attribute reference

In addition to all argument reference listed, you can access the following attribute references after your data source is created.

- `id` - (String) Workspace ID.
Expand All @@ -54,14 +47,14 @@ In addition to all argument reference listed, you can access the following attri
- `pi_workspace_details` - (Map) Workspace information.

Nested schema for `pi_workspace_details`:
- `creation_date` - (String) Workspace creation date.
- `creation_date` - (String) Date of workspace creation.
- `crn` - (String) Workspace crn.
- `pi_workspace_location` - (Map) Workspace location.

Nested schema for `Workspace location`:
- `region` - (String) The Workspace location region zone.
- `type` - (String) The Workspace location region type.
- `url`- (String) The Workspace location region url.
- `pi_workspace_name` - (String) The Workspace name.
- `pi_workspace_status` - (String) The Workspace status, `active`, `critical`, `failed`, `provisioning`.
- `pi_workspace_type` - (String) The Workspace type, `off-premises` or `on-premises`.
- `region` - (String) Workspace location region zone.
- `type` - (String) Workspace location region type.
- `url`- (String) Workspace location region url.
- `pi_workspace_name` - (String) Workspace name.
- `pi_workspace_status` - (String) Workspace status, `active`, `critical`, `failed`, `provisioning`.
- `pi_workspace_type` - (String) Workspace type, `off-premises` or `on-premises`.
Loading

0 comments on commit d727172

Please sign in to comment.