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

Terraform ProfitBricks Builder #7943

Merged
merged 12 commits into from
Jan 18, 2017
Merged
12 changes: 12 additions & 0 deletions builtin/bins/provider-profitbricks/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package provider_profitbricks

import (
"github.com/hashicorp/terraform/builtin/providers/profitbricks"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: profitbricks.Provider,
})
}
22 changes: 22 additions & 0 deletions builtin/providers/profitbricks/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package profitbricks

import (
"github.com/profitbricks/profitbricks-sdk-go"
"log"
"strconv"
)

type Config struct {
Username string
Password string
Timeout int
}

// Client() returns a new client for accessing digital ocean.
func (c *Config) Client() (string, error) {
profitbricks.SetAuth(c.Username, c.Password)
profitbricks.SetDepth("5")
log.Printf("[DEBUG] Username and password %s : %s", c.Username, c.Password)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should log the password here

log.Printf("[DEBUG] Timeout %d", c.Timeout)
return c.Username + "," + c.Password + "," + strconv.Itoa(c.Timeout), nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksDatacenter_importBasic(t *testing.T) {
resourceName := "profitbricks_datacenter.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksDatacenterDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitBricksDatacenterConfig_basic, "datacenter"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksFirewall_importBasic(t *testing.T) {
resourceName := "profitbricks_firewall.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksFirewallDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksFirewallConfig_basic, "firewall"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/profitbricks/import_profitbricks_ipblock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksIPBlock_importBasic(t *testing.T) {
resourceName := "profitbricks_ipblock.webserver_ip"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksIPBlockDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksIPBlockConfig_basic, "ipblock"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/profitbricks/import_profitbricks_lan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksLan_importBasic(t *testing.T) {
resourceName := "profitbricks_lan.webserver_lan"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksLanDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksLanConfig_basic, "lan"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksLoadbalancer_importBasic(t *testing.T) {
resourceName := "profitbricks_loadbalancer.example"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksLoadbalancerDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksLoadbalancerConfig_basic, "loadbalancer"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/profitbricks/import_profitbricks_nic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksNic_importBasic(t *testing.T) {
resourceName := "profitbricks_nic.database_nic"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksNicDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksNicConfig_basic, "nic"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
35 changes: 35 additions & 0 deletions builtin/providers/profitbricks/import_profitbricks_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksServer_importBasic(t *testing.T) {
resourceName := "profitbricks_server.webserver"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksServerDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksServerConfig_basic, "server"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"nic",
"volume",
"boot_volume",
},
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/profitbricks/import_profitbricks_volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package profitbricks

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccProfitBricksVolume_importBasic(t *testing.T) {
resourceName := "profitbricks_volume.database_volume"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksVolumeDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitbricksVolumeConfig_basic, "volume"),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
53 changes: 53 additions & 0 deletions builtin/providers/profitbricks/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package profitbricks

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Provider returns a schema.Provider for DigitalOcean.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_USERNAME", nil),
Description: "Profitbricks username for API operations.",
},
"password": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_PASSWORD", nil),
Description: "Profitbricks password for API operations.",
},
"timeout": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we set a default timeout?

Type: schema.TypeInt,
Optional: true,
},
},

ResourcesMap: map[string]*schema.Resource{
"profitbricks_datacenter": resourceProfitBricksDatacenter(),
"profitbricks_ipblock": resourceProfitBricksIPBlock(),
"profitbricks_firewall": resourceProfitBricksFirewall(),
"profitbricks_lan": resourceProfitBricksLan(),
"profitbricks_loadbalancer": resourceProfitBricksLoadbalancer(),
"profitbricks_nic": resourceProfitBricksNic(),
"profitbricks_server": resourceProfitBricksServer(),
"profitbricks_volume": resourceProfitBricksVolume(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Timeout: d.Get("timeout").(int),
Copy link
Contributor

Choose a reason for hiding this comment

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

We should check that this is set otherwise, it will be a 0 being passed - is that an issue?

}

return config.Client()
}
39 changes: 39 additions & 0 deletions builtin/providers/profitbricks/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package profitbricks

import (
"os"
"testing"

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

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"profitbricks": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("PROFITBRICKS_USERNAME"); v == "" {
t.Fatal("PROFITBRICKS_USERNAME must be set for acceptance tests")
}

if v := os.Getenv("PROFITBRICKS_PASSWORD"); v == "" {
t.Fatal("PROFITBRICKS_PASSWORD must be set for acceptance tests")
}
}
Loading