yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/service.go (about) 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 15 package azure 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 "time" 22 23 "yunion.io/x/log" 24 "yunion.io/x/pkg/errors" 25 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 ) 28 29 type SServices struct { 30 Value []SService `json:"value,omitempty"` 31 } 32 33 type SService struct { 34 ID string `json:"id,omitempty"` 35 Namespace string `json:"namespace,omitempty"` 36 RegistrationState string `json:"registrationState,omitempty"` 37 ResourceTypes []ResourceType `json:"resourceTypes,omitempty"` 38 } 39 40 type ResourceType struct { 41 ApiVersions []string `json:"apiVersions,omitempty"` 42 Capabilities string `json:"capabilities,omitempty"` 43 Locations []string `json:"locations,omitempty"` 44 ResourceType string `json:"resourceType,omitempty"` 45 } 46 47 func (self *SAzureClient) ListServices() ([]SService, error) { 48 services := []SService{} 49 return services, self.list("providers", url.Values{}, &services) 50 } 51 52 func (self *SAzureClient) GetSercice(serviceType string) (*SService, error) { 53 service := SService{} 54 return &service, self.get("providers/"+serviceType, url.Values{}, &service) 55 } 56 57 func (self *SAzureClient) serviceOperation(serviceType, operation string) error { 58 resource := fmt.Sprintf("subscriptions/%s/providers/%s", self.subscriptionId, serviceType) 59 _, err := self.perform(resource, operation, nil) 60 return err 61 } 62 63 func (self *SAzureClient) waitServiceStatus(serviceType, status string) error { 64 return cloudprovider.Wait(time.Second*10, time.Minute*5, func() (bool, error) { 65 services, err := self.ListServices() 66 if err != nil { 67 return false, errors.Wrapf(err, "ListServices") 68 } 69 for _, service := range services { 70 if strings.ToLower(service.Namespace) == strings.ToLower(serviceType) { 71 if service.RegistrationState == status { 72 return true, nil 73 } 74 log.Debugf("service %s status: %s expect %s", serviceType, service.RegistrationState, status) 75 } 76 } 77 return false, nil 78 }) 79 } 80 81 func (self *SAzureClient) ServiceRegister(serviceType string) error { 82 err := self.serviceOperation(serviceType, "register") 83 if err != nil { 84 return errors.Wrapf(err, "serviceOperation(%s)", "register") 85 } 86 return self.waitServiceStatus(serviceType, "Registered") 87 } 88 89 func (self *SAzureClient) ServiceUnRegister(serviceType string) error { 90 err := self.serviceOperation(serviceType, "unregister") 91 if err != nil { 92 return errors.Wrapf(err, "serviceOperation(%s)", "unregister") 93 } 94 return self.waitServiceStatus(serviceType, "NotRegistered") 95 }