Skip to content

Commit

Permalink
vultr: add bare metal resources
Browse files Browse the repository at this point in the history
  • Loading branch information
squat committed Apr 13, 2018
1 parent f0dffb6 commit 78a4f96
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 8 deletions.
38 changes: 38 additions & 0 deletions examples/bare_metal/example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Configure the Vultr provider.
// Alternatively, export the API key as an environment variable: `export VULTR_API_KEY=<your-vultr-api-key>`.
provider "vultr" {
api_key = "<your-vultr-api-key>"
}

// Find the OS ID for Container Linux.
data "vultr_os" "container_linux" {
filter {
name = "family"
values = ["coreos"]
}
}

// Find the ID of the Silicon Valley region.
data "vultr_region" "silicon_valley" {
filter {
name = "name"
values = ["Silicon Valley"]
}
}

// Find the ID for a starter plan.
data "vultr_bare_metal_plan" "eightcpus" {
filter {
name = "cpu_count"
values = [8]
}
}

// Create a Vultr virtual machine.
resource "vultr_bare_metal" "example" {
name = "example"
hostname = "example"
region_id = "${data.vultr_region.silicon_valley.id}"
plan_id = "${data.vultr_bare_metal_plan.eightcpus.id}"
os_id = "${data.vultr_os.container_linux.id}"
}
123 changes: 123 additions & 0 deletions vultr/data_source_bare_metal_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package vultr

import (
"errors"
"fmt"
"regexp"
"strconv"

"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceBareMetalPlan() *schema.Resource {
return &schema.Resource{
Read: dataSourceBareMetalPlanRead,

Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),

"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateRegex,
},

"available_locations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},

"bandwidth": {
Type: schema.TypeInt,
Computed: true,
},

"disk": {
Type: schema.TypeString,
Computed: true,
},

"name": {
Type: schema.TypeString,
Computed: true,
},

"price_per_month": {
Type: schema.TypeInt,
Computed: true,
},

"ram": {
Type: schema.TypeInt,
Computed: true,
},

"cpu_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

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

filters, filtersOk := d.GetOk("filter")
nameRegex, nameRegexOk := d.GetOk("name_regex")

if !filtersOk && !nameRegexOk {
return fmt.Errorf("One of %q and %q must be provided", "filter", "name_regex")
}

bareMetalPlans, err := client.GetBareMetalPlans()
if err != nil {
return fmt.Errorf("Error getting bare metal plans: %v", err)
}

if filtersOk {
filter := filterFromSet(filters.(*schema.Set))
var filteredBareMetalPlans []lib.BareMetalPlan
for _, bareMetalPlan := range bareMetalPlans {
m := structToMap(bareMetalPlan)
if filter.F(m) {
filteredBareMetalPlans = append(filteredBareMetalPlans, bareMetalPlan)
}
}
bareMetalPlans = filteredBareMetalPlans
}

if nameRegexOk {
var filteredBareMetalPlans []lib.BareMetalPlan
r := regexp.MustCompile(nameRegex.(string))
for _, bareMetalPlan := range bareMetalPlans {
if r.MatchString(bareMetalPlan.Name) {
filteredBareMetalPlans = append(filteredBareMetalPlans, bareMetalPlan)
}
}
bareMetalPlans = filteredBareMetalPlans
}

if len(bareMetalPlans) < 1 {
return errors.New("The query for bare metal plans returned no results. Please modify the search criteria and try again")
}

if len(bareMetalPlans) > 1 {
return fmt.Errorf("The query for bare metal plans returned %d results. Please make the search criteria more specific and try again", len(bareMetalPlans))
}

d.SetId(strconv.Itoa(bareMetalPlans[0].ID))
d.Set("available_locations", bareMetalPlans[0].Regions)
d.Set("bandwidth", bareMetalPlans[0].Bandwidth)
d.Set("disk", bareMetalPlans[0].Disk)
d.Set("name", bareMetalPlans[0].Name)
d.Set("price_per_month", bareMetalPlans[0].Price)
d.Set("ram", bareMetalPlans[0].RAM)
d.Set("cpu_count", bareMetalPlans[0].CPUs)
return nil
}
18 changes: 10 additions & 8 deletions vultr/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"vultr_application": dataSourceApplication(),
"vultr_firewall_group": dataSourceFirewallGroup(),
"vultr_os": dataSourceOS(),
"vultr_plan": dataSourcePlan(),
"vultr_region": dataSourceRegion(),
"vultr_snapshot": dataSourceSnapshot(),
"vultr_ssh_key": dataSourceSSHKey(),
"vultr_application": dataSourceApplication(),
"vultr_bare_metal_plan": dataSourceBareMetalPlan(),
"vultr_firewall_group": dataSourceFirewallGroup(),
"vultr_os": dataSourceOS(),
"vultr_plan": dataSourcePlan(),
"vultr_region": dataSourceRegion(),
"vultr_snapshot": dataSourceSnapshot(),
"vultr_ssh_key": dataSourceSSHKey(),
},

ResourcesMap: map[string]*schema.Resource{
"vultr_block_storage": resourceBlockStorage(),
"vultr_bare_metal": resourceBareMetal(),
"vultr_dns_domain": resourceDNSDomain(),
"vultr_dns_record": resourceDNSRecord(),
"vultr_firewall_group": resourceFirewallGroup(),
Expand All @@ -37,7 +40,6 @@ func Provider() terraform.ResourceProvider {
"vultr_ipv4": resourceIPV4(),
"vultr_startup_script": resourceStartupScript(),
"vultr_ssh_key": resourceSSHKey(),
"vultr_block_storage": resourceBlockStorage(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading

0 comments on commit 78a4f96

Please sign in to comment.