refactor service clients to *Config

This commit is contained in:
Jon Perritt 2015-01-31 14:33:54 -07:00
parent 3627368fc0
commit 1aba665ad7
10 changed files with 47 additions and 121 deletions

View File

@ -41,3 +41,15 @@ func (c *Config) loadAndValidate() error {
return nil
}
func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{
Region: region,
})
}
func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{
Region: region,
})
}

View File

@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
@ -120,9 +119,7 @@ func resourceComputeInstanceV2() *schema.Resource {
func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -185,9 +182,7 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e
func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -254,9 +249,7 @@ func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) err
func resourceComputeInstanceV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -382,9 +375,7 @@ func resourceComputeInstanceV2Update(d *schema.ResourceData, meta interface{}) e
func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}

View File

@ -4,8 +4,6 @@ import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
)
@ -38,9 +36,7 @@ func resourceComputeKeypairV2() *schema.Resource {
func resourceComputeKeypairV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -62,9 +58,7 @@ func resourceComputeKeypairV2Create(d *schema.ResourceData, meta interface{}) er
func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -83,9 +77,7 @@ func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) erro
func resourceComputeKeypairV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}

View File

@ -7,8 +7,6 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
)
@ -76,9 +74,7 @@ func resourceComputeSecGroupV2() *schema.Resource {
func resourceComputeSecGroupV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -108,9 +104,7 @@ func resourceComputeSecGroupV2Create(d *schema.ResourceData, meta interface{}) e
func resourceComputeSecGroupV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -130,9 +124,7 @@ func resourceComputeSecGroupV2Read(d *schema.ResourceData, meta interface{}) err
func resourceComputeSecGroupV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
@ -183,9 +175,7 @@ func resourceComputeSecGroupV2Update(d *schema.ResourceData, meta interface{}) e
func resourceComputeSecGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}

View File

@ -5,8 +5,6 @@ import (
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/members"
)
@ -55,9 +53,7 @@ func resourceLBMemberV1() *schema.Resource {
func resourceLBMemberV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -83,9 +79,7 @@ func resourceLBMemberV1Create(d *schema.ResourceData, meta interface{}) error {
func resourceLBMemberV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -113,9 +107,7 @@ func resourceLBMemberV1Read(d *schema.ResourceData, meta interface{}) error {
func resourceLBMemberV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -137,9 +129,7 @@ func resourceLBMemberV1Update(d *schema.ResourceData, meta interface{}) error {
func resourceLBMemberV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

View File

@ -6,8 +6,6 @@ import (
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
)
@ -76,9 +74,7 @@ func resourceLBMonitorV1() *schema.Resource {
func resourceLBMonitorV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -117,9 +113,7 @@ func resourceLBMonitorV1Create(d *schema.ResourceData, meta interface{}) error {
func resourceLBMonitorV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -172,9 +166,7 @@ func resourceLBMonitorV1Read(d *schema.ResourceData, meta interface{}) error {
func resourceLBMonitorV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -221,9 +213,7 @@ func resourceLBMonitorV1Update(d *schema.ResourceData, meta interface{}) error {
func resourceLBMonitorV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

View File

@ -6,8 +6,6 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
)
@ -66,9 +64,7 @@ func resourceLBPoolV1() *schema.Resource {
func resourceLBPoolV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -104,9 +100,7 @@ func resourceLBPoolV1Create(d *schema.ResourceData, meta interface{}) error {
func resourceLBPoolV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -137,9 +131,7 @@ func resourceLBPoolV1Read(d *schema.ResourceData, meta interface{}) error {
func resourceLBPoolV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -191,9 +183,7 @@ func resourceLBPoolV1Update(d *schema.ResourceData, meta interface{}) error {
func resourceLBPoolV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

View File

@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
)
@ -86,9 +85,7 @@ func resourceLBVipV1() *schema.Resource {
func resourceLBVipV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -129,9 +126,7 @@ func resourceLBVipV1Create(d *schema.ResourceData, meta interface{}) error {
func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -189,9 +184,7 @@ func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error {
func resourceLBVipV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -235,9 +228,7 @@ func resourceLBVipV1Update(d *schema.ResourceData, meta interface{}) error {
func resourceLBVipV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

View File

@ -6,8 +6,6 @@ import (
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
)
@ -51,9 +49,7 @@ func resourceNetworkingNetworkV2() *schema.Resource {
func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -95,9 +91,7 @@ func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{})
func resourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -140,9 +134,7 @@ func resourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) e
func resourceNetworkingNetworkV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -184,9 +176,7 @@ func resourceNetworkingNetworkV2Update(d *schema.ResourceData, meta interface{})
func resourceNetworkingNetworkV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

View File

@ -7,8 +7,6 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
)
@ -110,9 +108,7 @@ func resourceNetworkingSubnetV2() *schema.Resource {
func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -152,9 +148,7 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{})
func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -212,9 +206,7 @@ func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) er
func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
@ -260,9 +252,7 @@ func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{})
func resourceNetworkingSubnetV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}