Handling missing resources (#13053)

This commit is contained in:
Jasmin Gacic 2017-03-25 21:43:41 +01:00 committed by Paul Stack
parent e50fbfe779
commit d899709004
11 changed files with 53 additions and 10 deletions

View File

@ -11,7 +11,7 @@ type Config struct {
Retries int
}
// Client() returns a new client for accessing digital ocean.
// Client() returns a new client for accessing ProfitBricks.
func (c *Config) Client() (*Config, error) {
profitbricks.SetAuth(c.Username, c.Password)
profitbricks.SetDepth("5")

View File

@ -7,7 +7,7 @@ import (
"github.com/profitbricks/profitbricks-sdk-go"
)
// Provider returns a schema.Provider for DigitalOcean.
// Provider returns a schema.Provider for ProfitBricks.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
@ -15,19 +15,19 @@ func Provider() terraform.ResourceProvider {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_USERNAME", nil),
Description: "Profitbricks username for API operations.",
Description: "ProfitBricks username for API operations.",
},
"password": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_PASSWORD", nil),
Description: "Profitbricks password for API operations.",
Description: "ProfitBricks password for API operations.",
},
"endpoint": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_API_URL", profitbricks.Endpoint),
Description: "Profitbricks REST API URL.",
Description: "ProfitBricks REST API URL.",
},
"retries": {
Type: schema.TypeInt,

View File

@ -69,6 +69,10 @@ func resourceProfitBricksDatacenterCreate(d *schema.ResourceData, meta interface
func resourceProfitBricksDatacenterRead(d *schema.ResourceData, meta interface{}) error {
datacenter := profitbricks.GetDatacenter(d.Id())
if datacenter.StatusCode > 299 {
if datacenter.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error while fetching a data center ID %s %s", d.Id(), datacenter.Response)
}

View File

@ -11,7 +11,7 @@ import (
func TestAccProfitBricksDataCenter_Basic(t *testing.T) {
var datacenter profitbricks.Datacenter
lanName := "datacenter-test"
dc_name := "datacenter-test"
resource.Test(t, resource.TestCase{
PreCheck: func() {
@ -21,11 +21,11 @@ func TestAccProfitBricksDataCenter_Basic(t *testing.T) {
CheckDestroy: testAccCheckDProfitBricksDatacenterDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckProfitBricksDatacenterConfig_basic, lanName),
Config: fmt.Sprintf(testAccCheckProfitBricksDatacenterConfig_basic, dc_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfitBricksDatacenterExists("profitbricks_datacenter.foobar", &datacenter),
testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", lanName),
resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", lanName),
testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", dc_name),
resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", dc_name),
),
},
resource.TestStep{

View File

@ -131,6 +131,10 @@ func resourceProfitBricksFirewallRead(d *schema.ResourceData, meta interface{})
fw := profitbricks.GetFirewallRule(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id())
if fw.StatusCode > 299 {
if fw.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a firewall rule dcId: %s server_id: %s nic_id: %s ID: %s %s", d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id(), fw.Response)
}

View File

@ -59,6 +59,10 @@ func resourceProfitBricksIPBlockRead(d *schema.ResourceData, meta interface{}) e
ipblock := profitbricks.GetIpBlock(d.Id())
if ipblock.StatusCode > 299 {
if ipblock.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), ipblock.Response)
}

View File

@ -65,6 +65,10 @@ func resourceProfitBricksLanRead(d *schema.ResourceData, meta interface{}) error
lan := profitbricks.GetLan(d.Get("datacenter_id").(string), d.Id())
if lan.StatusCode > 299 {
if lan.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lan.Response)
}

View File

@ -75,6 +75,14 @@ func resourceProfitBricksLoadbalancerCreate(d *schema.ResourceData, meta interfa
func resourceProfitBricksLoadbalancerRead(d *schema.ResourceData, meta interface{}) error {
lb := profitbricks.GetLoadbalancer(d.Get("datacenter_id").(string), d.Id())
if lb.StatusCode > 299 {
if lb.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lb.Response)
}
d.Set("name", lb.Properties.Name)
d.Set("ip", lb.Properties.Ip)
d.Set("dhcp", lb.Properties.Dhcp)

View File

@ -109,6 +109,10 @@ func resourceProfitBricksNicCreate(d *schema.ResourceData, meta interface{}) err
func resourceProfitBricksNicRead(d *schema.ResourceData, meta interface{}) error {
nic := profitbricks.GetNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id())
if nic.StatusCode > 299 {
if nic.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error occured while fetching a nic ID %s %s", d.Id(), nic.Response)
}
log.Printf("[INFO] LAN ON NIC: %d", nic.Properties.Lan)

View File

@ -449,7 +449,13 @@ func resourceProfitBricksServerRead(d *schema.ResourceData, meta interface{}) er
serverId := d.Id()
server := profitbricks.GetServer(dcId, serverId)
if server.StatusCode > 299 {
if server.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error occured while fetching a server ID %s %s", d.Id(), server.Response)
}
d.Set("name", server.Properties.Name)
d.Set("cores", server.Properties.Cores)
d.Set("ram", server.Properties.Ram)

View File

@ -163,6 +163,15 @@ func resourceProfitBricksVolumeRead(d *schema.ResourceData, meta interface{}) er
dcId := d.Get("datacenter_id").(string)
volume := profitbricks.GetVolume(dcId, d.Id())
if volume.StatusCode > 299 {
if volume.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error occured while fetching a volume ID %s %s", d.Id(), volume.Response)
}
if volume.StatusCode > 299 {
return fmt.Errorf("An error occured while fetching a volume ID %s %s", d.Id(), volume.Response)