openstack_client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package openstack
  14. import (
  15. "fmt"
  16. "github.com/gophercloud/gophercloud"
  17. "github.com/gophercloud/gophercloud/openstack"
  18. )
  19. // NewNetworkV2 creates a ServiceClient that may be used with the neutron v2 API
  20. func (os *OpenStack) NewNetworkV2() (*gophercloud.ServiceClient, error) {
  21. network, err := openstack.NewNetworkV2(os.provider, gophercloud.EndpointOpts{
  22. Region: os.region,
  23. })
  24. if err != nil {
  25. return nil, fmt.Errorf("failed to find network v2 endpoint for region %s: %v", os.region, err)
  26. }
  27. return network, nil
  28. }
  29. // NewComputeV2 creates a ServiceClient that may be used with the nova v2 API
  30. func (os *OpenStack) NewComputeV2() (*gophercloud.ServiceClient, error) {
  31. compute, err := openstack.NewComputeV2(os.provider, gophercloud.EndpointOpts{
  32. Region: os.region,
  33. })
  34. if err != nil {
  35. return nil, fmt.Errorf("failed to find compute v2 endpoint for region %s: %v", os.region, err)
  36. }
  37. return compute, nil
  38. }
  39. // NewBlockStorageV1 creates a ServiceClient that may be used with the Cinder v1 API
  40. func (os *OpenStack) NewBlockStorageV1() (*gophercloud.ServiceClient, error) {
  41. storage, err := openstack.NewBlockStorageV1(os.provider, gophercloud.EndpointOpts{
  42. Region: os.region,
  43. })
  44. if err != nil {
  45. return nil, fmt.Errorf("unable to initialize cinder v1 client for region %s: %v", os.region, err)
  46. }
  47. return storage, nil
  48. }
  49. // NewBlockStorageV2 creates a ServiceClient that may be used with the Cinder v2 API
  50. func (os *OpenStack) NewBlockStorageV2() (*gophercloud.ServiceClient, error) {
  51. storage, err := openstack.NewBlockStorageV2(os.provider, gophercloud.EndpointOpts{
  52. Region: os.region,
  53. })
  54. if err != nil {
  55. return nil, fmt.Errorf("unable to initialize cinder v2 client for region %s: %v", os.region, err)
  56. }
  57. return storage, nil
  58. }
  59. // NewBlockStorageV3 creates a ServiceClient that may be used with the Cinder v3 API
  60. func (os *OpenStack) NewBlockStorageV3() (*gophercloud.ServiceClient, error) {
  61. storage, err := openstack.NewBlockStorageV3(os.provider, gophercloud.EndpointOpts{
  62. Region: os.region,
  63. })
  64. if err != nil {
  65. return nil, fmt.Errorf("unable to initialize cinder v3 client for region %s: %v", os.region, err)
  66. }
  67. return storage, nil
  68. }
  69. // NewLoadBalancerV2 creates a ServiceClient that may be used with the Neutron LBaaS v2 API
  70. func (os *OpenStack) NewLoadBalancerV2() (*gophercloud.ServiceClient, error) {
  71. var lb *gophercloud.ServiceClient
  72. var err error
  73. if os.lbOpts.UseOctavia {
  74. lb, err = openstack.NewLoadBalancerV2(os.provider, gophercloud.EndpointOpts{
  75. Region: os.region,
  76. })
  77. } else {
  78. lb, err = openstack.NewNetworkV2(os.provider, gophercloud.EndpointOpts{
  79. Region: os.region,
  80. })
  81. }
  82. if err != nil {
  83. return nil, fmt.Errorf("failed to find load-balancer v2 endpoint for region %s: %v", os.region, err)
  84. }
  85. return lb, nil
  86. }