Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Data Source: aws_workspaces_bundle #3243

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/aws/aws-sdk-go/service/wafregional"
"github.com/aws/aws-sdk-go/service/workspaces"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-cleanhttp"
Expand Down Expand Up @@ -206,6 +207,7 @@ type AWSClient struct {
athenaconn *athena.Athena
dxconn *directconnect.DirectConnect
mediastoreconn *mediastore.MediaStore
wsconn *workspaces.WorkSpaces
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: to prevent any confusion with other services, we should probably name this workspacesconn

}

func (c *AWSClient) S3() *s3.S3 {
Expand Down Expand Up @@ -451,6 +453,7 @@ func (c *Config) Client() (interface{}, error) {
client.athenaconn = athena.New(sess)
client.dxconn = directconnect.New(sess)
client.mediastoreconn = mediastore.New(sess)
client.wsconn = workspaces.New(sess)

// Workaround for https://github.com/aws/aws-sdk-go/issues/1376
client.kinesisconn.Handlers.Retry.PushBack(func(r *request.Request) {
Expand Down
118 changes: 118 additions & 0 deletions aws/data_source_aws_workspaces_bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/workspaces"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsWorkspaceBundle() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsWorkspaceBundleRead,

Schema: map[string]*schema.Schema{
"bundle_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ForceNew is not required for data sources and should be removed from this attribute and the documentation.

},
"description": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"compute_type": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"user_storage": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"capacity": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"root_storage": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"capacity": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceAwsWorkspaceBundleRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wsconn

bundleID := d.Get("bundle_id").(string)
input := &workspaces.DescribeWorkspaceBundlesInput{
BundleIds: []*string{aws.String(bundleID)},
}

resp, err := conn.DescribeWorkspaceBundles(input)
if err != nil {
return err
}

if len(resp.Bundles) != 1 {
return fmt.Errorf("The number of Workspace Bundle (%s) should be 1, but %d", bundleID, len(resp.Bundles))
}

bundle := resp.Bundles[0]
d.SetId(bundleID)
d.Set("description", bundle.Description)
d.Set("name", bundle.Name)
d.Set("owner", bundle.Owner)
if bundle.ComputeType != nil {
r := map[string]interface{}{
"name": *bundle.ComputeType.Name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics, we should prefer to use the SDK provided function for dereferencing values (e.g. aws.StringValue(bundle.ComputeType.Name)) instead of directly dereferencing via *

This needs to be repeated for bundle.RootStorage.Capacity and bundle.UserStorage.Capacity below as well.

}
ct := []map[string]interface{}{r}
d.Set("compute_type", ct)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using d.Set() with aggregate types (TypeList, TypeSet, TypeMap), we should perform error checking to prevent issues where the code is not properly able to set the Terraform state. e.g.

if err := d.Set("compute_type", ct); err != nil {
  return fmt.Errorf("error setting compute_type: %s", err)
}

This also needs to be repeated for root_storage and user_storage below.

}
if bundle.RootStorage != nil {
r := map[string]interface{}{
"capacity": *bundle.RootStorage.Capacity,
}
rs := []map[string]interface{}{r}
d.Set("root_storage", rs)
}
if bundle.UserStorage != nil {
r := map[string]interface{}{
"capacity": *bundle.UserStorage.Capacity,
}
us := []map[string]interface{}{r}
d.Set("user_storage", us)
}

return nil
}
32 changes: 32 additions & 0 deletions aws/data_source_aws_workspaces_bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsWorkspaceBundle_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsWorkspaceBundleConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_workspaces_bundle.test", "bundle_id", "wsb-b0s22j3d7"),
resource.TestCheckResourceAttrSet(
"data.aws_workspaces_bundle.test", "name"),
resource.TestCheckResourceAttrSet(
"data.aws_workspaces_bundle.test", "owner"),
),
},
},
})
}

const testAccDataSourceAwsWorkspaceBundleConfig = `
data "aws_workspaces_bundle" "test" {
bundle_id = "wsb-b0s22j3d7"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func Provider() terraform.ResourceProvider {
"aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(),
"aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(),
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_workspaces_bundle": dataSourceAwsWorkspaceBundle(),

// Adding the Aliases for the ALB -> LB Rename
"aws_lb": dataSourceAwsLb(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@
<li<%= sidebar_current("docs-aws-datasource-vpn-gateway") %>>
<a href="/docs/providers/aws/d/vpn_gateway.html">aws_vpn_gateway</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-workspaces-bundle") %>>
<a href="/docs/providers/aws/d/workspaces_bundle.html">aws_workspaces_bundle</a>
</li>
</ul>
</li>

Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/workspaces_bundle.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
layout: "aws"
page_title: "AWS: aws_workspaces_bundle"
sidebar_current: "docs-aws-datasource-workspaces-bundle"
description: |-
Get information on a Workspaces Bundle.
---

# aws_workspaces_bundle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely a change that occurred after the creation of this pull request, but can you please prepend the title with Data Source: , e.g. Data Source: aws_workspaces_bundle


Use this data source to get information about a Workspaces Bundle.

## Example Usage

```hcl
data "aws_workspaces_bundle" "example" {
bundle_id = "wsb-b0s22j3d7"
}
```

## Argument Reference

The following arguments are supported:

* `bundle_id` – (Required, ForceNew) The ID of the bundle.

## Attributes Reference

The following attributes are exported:

* `description` – The description of the bundle.
* `name` – The name of the bundle.
* `owner` – The owner of the bundle.
* `compute_type` – The compute type. See supported fields below.
* `root_storage` – The root volume. See supported fields below.
* `user_storage` – The user storage. See supported fields below.

### `compute_type`

* `name` - The name of the compute type.

### `root_storage`

* `capacity` - The size of the root volume.

### `user_storage`

* `capacity` - The size of the user storage.