service.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. )
  24. type SServices struct {
  25. Value []SService `json:"value,omitempty"`
  26. }
  27. type SService struct {
  28. ID string `json:"id,omitempty"`
  29. Namespace string `json:"namespace,omitempty"`
  30. RegistrationState string `json:"registrationState,omitempty"`
  31. ResourceTypes []ResourceType `json:"resourceTypes,omitempty"`
  32. }
  33. type ResourceType struct {
  34. ApiVersions []string `json:"apiVersions,omitempty"`
  35. Capabilities string `json:"capabilities,omitempty"`
  36. Locations []string `json:"locations,omitempty"`
  37. ResourceType string `json:"resourceType,omitempty"`
  38. }
  39. func (self *SAzureClient) ListServices() ([]SService, error) {
  40. services := []SService{}
  41. return services, self.list("providers", url.Values{}, &services)
  42. }
  43. func (self *SAzureClient) GetSercice(serviceType string) (*SService, error) {
  44. service := SService{}
  45. return &service, self.get("providers/"+serviceType, url.Values{}, &service)
  46. }
  47. func (self *SAzureClient) serviceOperation(serviceType, operation string) error {
  48. resource := fmt.Sprintf("subscriptions/%s/providers/%s", self.subscriptionId, serviceType)
  49. _, err := self.perform(resource, operation, nil)
  50. return err
  51. }
  52. func (self *SAzureClient) waitServiceStatus(serviceType, status string) error {
  53. return cloudprovider.Wait(time.Second*10, time.Minute*5, func() (bool, error) {
  54. services, err := self.ListServices()
  55. if err != nil {
  56. return false, errors.Wrapf(err, "ListServices")
  57. }
  58. for _, service := range services {
  59. if strings.ToLower(service.Namespace) == strings.ToLower(serviceType) {
  60. if service.RegistrationState == status {
  61. return true, nil
  62. }
  63. log.Debugf("service %s status: %s expect %s", serviceType, service.RegistrationState, status)
  64. }
  65. }
  66. return false, nil
  67. })
  68. }
  69. func (self *SAzureClient) ServiceRegister(serviceType string) error {
  70. err := self.serviceOperation(serviceType, "register")
  71. if err != nil {
  72. return errors.Wrapf(err, "serviceOperation(%s)", "register")
  73. }
  74. return self.waitServiceStatus(serviceType, "Registered")
  75. }
  76. func (self *SAzureClient) ServiceUnRegister(serviceType string) error {
  77. err := self.serviceOperation(serviceType, "unregister")
  78. if err != nil {
  79. return errors.Wrapf(err, "serviceOperation(%s)", "unregister")
  80. }
  81. return self.waitServiceStatus(serviceType, "NotRegistered")
  82. }