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

triton_image: New data source #7

Merged
merged 3 commits into from
Jul 15, 2017
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
103 changes: 103 additions & 0 deletions triton/data_source_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package triton

import (
"context"
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/triton-go"
)

func dataSourceImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceImageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"os": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"public": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"state": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"owner": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}

func dataSourceImageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*triton.Client)

input := &triton.ListImagesInput{}
if name, hasName := d.GetOk("name"); hasName {
input.Name = name.(string)
}
if os, hasOS := d.GetOk("os"); hasOS {
input.OS = os.(string)
}
if version, hasVersion := d.GetOk("version"); hasVersion {
input.Version = version.(string)
}
if public, hasPublic := d.GetOk("public"); hasPublic {
input.Public = public.(bool)
}
if state, hasState := d.GetOk("state"); hasState {
input.State = state.(string)
}
if owner, hasOwner := d.GetOk("owner"); hasOwner {
input.State = owner.(string)
}
if imageType, hasImageType := d.GetOk("type"); hasImageType {
input.State = imageType.(string)
}

images, err := client.Images().ListImages(context.Background(), input)
if err != nil {
return err
}

if len(images) == 0 {
return fmt.Errorf("Your query returned no results. Please change " +
"your search criteria and try again.")
}

if len(images) > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

d.SetId(images[0].ID)
return nil
}
52 changes: 52 additions & 0 deletions triton/data_source_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package triton

import (
"fmt"
"testing"

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

func TestAccTritonImage_basic(t *testing.T) {
const base64LTS = "1f32508c-e6e9-11e6-bc05-8fea9e979940"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTritonImage_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckTritonImageDataSourceID("data.triton_image.base", base64LTS),
),
},
},
})
}

func testAccCheckTritonImageDataSourceID(name, id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Can't find Image data source: %s", name)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Image data source ID not set")
}

if rs.Primary.ID != id {
return fmt.Errorf("Bad ID for data source %q: expected %q, got %q",
name, id, rs.Primary.ID)
}
return nil
}
}

var testAccTritonImage_basic = `
data "triton_image" "base" {
name = "base-64-lts"
version = "16.4.1"
}
`
4 changes: 4 additions & 0 deletions triton/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func Provider() terraform.ResourceProvider {
},
},

DataSourcesMap: map[string]*schema.Resource{
"triton_image": dataSourceImage(),
},

ResourcesMap: map[string]*schema.Resource{
"triton_firewall_rule": resourceFirewallRule(),
"triton_machine": resourceMachine(),
Expand Down
5 changes: 4 additions & 1 deletion triton/resource_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"fmt"
"regexp"
"strconv"
"time"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/triton-go"
"github.com/mitchellh/hashstructure"
)

var (
Expand Down Expand Up @@ -431,7 +433,8 @@ func resourceMachineUpdate(d *schema.ResourceData, meta interface{}) error {
return nil, "", err
}

return getResp, stableMapHash(getResp.Tags), nil
hash, err := hashstructure.Hash(getResp.Tags, nil)
return getResp, strconv.FormatUint(hash, 10), err
},
Timeout: machineStateChangeTimeout,
MinTimeout: 3 * time.Second,
Expand Down
6 changes: 3 additions & 3 deletions vendor/github.com/joyent/triton-go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 38 additions & 6 deletions vendor/github.com/joyent/triton-go/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/joyent/triton-go/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 35 additions & 3 deletions vendor/github.com/joyent/triton-go/images.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading