This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openstack_client.go
129 lines (107 loc) · 3.87 KB
/
openstack_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package client
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
"github.com/frodenas/bosh-openstack-cpi/openstack/config"
"github.com/frodenas/bosh-openstack-cpi/openstack/extension_service"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
)
type OpenStackClient struct {
config config.Config
computeService *gophercloud.ServiceClient
blockstorageService *gophercloud.ServiceClient
networkService *gophercloud.ServiceClient
computeExtensions extension.OpenStackComputeExtensions
networkExtensions extension.OpenStackNetworkExtensions
logger boshlog.Logger
}
func NewOpenStackClient(
config config.Config,
logger boshlog.Logger,
) (OpenStackClient, error) {
authOptions := gophercloud.AuthOptions{
IdentityEndpoint: config.IdentityEndpoint,
Username: config.Username,
UserID: config.UserID,
Password: config.Password,
APIKey: config.APIKey,
TenantName: config.TenantName,
TenantID: config.TenantID,
DomainID: config.DomainID,
DomainName: config.DomainName,
AllowReauth: true,
}
providerClient, err := openstack.AuthenticatedClient(authOptions)
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Authenticating client")
}
endpointOpts := gophercloud.EndpointOpts{
Region: config.Region,
}
computeService, err := openstack.NewComputeV2(providerClient, endpointOpts)
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Creating Compute Service")
}
computeExtensionService := extension.NewOpenStackComputeExtensionService(computeService, logger)
computeExtensions, err := computeExtensionService.List()
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Creating Compute Service")
}
blockstorageService, err := openstack.NewBlockStorageV1(providerClient, endpointOpts)
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Creating Block Storage Service")
}
var networkService *gophercloud.ServiceClient
var networkExtensions map[string]struct{}
if !config.DisableNeutron {
networkService, err = openstack.NewNetworkV2(providerClient, endpointOpts)
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Creating Network Service")
}
networkExtensionService := extension.NewOpenStackNetworkExtensionService(networkService, logger)
networkExtensions, err = networkExtensionService.List()
if err != nil {
return OpenStackClient{}, bosherr.WrapError(err, "Creating Compute Service")
}
}
return OpenStackClient{
config: config,
computeService: computeService,
blockstorageService: blockstorageService,
networkService: networkService,
computeExtensions: computeExtensions,
networkExtensions: networkExtensions,
logger: logger,
}, nil
}
func (c OpenStackClient) ComputeService() *gophercloud.ServiceClient {
return c.computeService
}
func (c OpenStackClient) BlockStorageService() *gophercloud.ServiceClient {
return c.blockstorageService
}
func (c OpenStackClient) NetworkService() *gophercloud.ServiceClient {
return c.networkService
}
func (c OpenStackClient) ComputeExtensions() extension.OpenStackComputeExtensions {
return c.computeExtensions
}
func (c OpenStackClient) NetworkExtensions() extension.OpenStackNetworkExtensions {
return c.networkExtensions
}
func (c OpenStackClient) DefaultKeyPair() string {
return c.config.DefaultKeyPair
}
func (c OpenStackClient) DefaultSecurityGroups() []string {
return c.config.DefaultSecurityGroups
}
func (c OpenStackClient) DisableConfigDrive() bool {
return c.config.DisableConfigDrive
}
func (c OpenStackClient) DisableNeutron() bool {
return c.config.DisableNeutron
}
func (c OpenStackClient) IgnoreServerAvailabilityZone() bool {
return c.config.IgnoreServerAvailabilityZone
}