yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/provider/provider.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 provider 16 17 import ( 18 "context" 19 "fmt" 20 "net/url" 21 "strconv" 22 "strings" 23 24 "yunion.io/x/jsonutils" 25 "yunion.io/x/pkg/errors" 26 "yunion.io/x/pkg/util/regutils" 27 28 api "yunion.io/x/cloudmux/pkg/apis/compute" 29 "yunion.io/x/cloudmux/pkg/cloudprovider" 30 "yunion.io/x/onecloud/pkg/httperrors" 31 "yunion.io/x/onecloud/pkg/mcclient" 32 "yunion.io/x/cloudmux/pkg/multicloud/nutanix" 33 ) 34 35 type SNutanixProviderFactory struct { 36 cloudprovider.SPrivateCloudBaseProviderFactory 37 } 38 39 func (self *SNutanixProviderFactory) GetId() string { 40 return nutanix.CLOUD_PROVIDER_NUTANIX 41 } 42 43 func (self *SNutanixProviderFactory) GetName() string { 44 return nutanix.CLOUD_PROVIDER_NUTANIX 45 } 46 47 func (self *SNutanixProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error { 48 return fmt.Errorf("Changing %s bandwidth is not supported", nutanix.CLOUD_PROVIDER_NUTANIX) 49 } 50 51 func (self *SNutanixProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 52 output := cloudprovider.SCloudaccount{} 53 if len(input.Username) == 0 { 54 return output, errors.Wrap(httperrors.ErrMissingParameter, "username") 55 } 56 if len(input.Password) == 0 { 57 return output, errors.Wrap(httperrors.ErrMissingParameter, "password") 58 } 59 if len(input.Host) == 0 { 60 return output, errors.Wrap(httperrors.ErrMissingParameter, "host") 61 } 62 if !regutils.MatchIPAddr(input.Host) && !regutils.MatchDomainName(input.Host) { 63 return output, errors.Wrap(httperrors.ErrInputParameter, "host should be ip or domain name") 64 } 65 if input.Port == 0 { 66 input.Port = 9440 67 } 68 output.AccessUrl = fmt.Sprintf("https://%s:%d", input.Host, input.Port) 69 output.Account = input.Username 70 output.Secret = input.Password 71 return output, nil 72 } 73 74 func (self *SNutanixProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) { 75 output := cloudprovider.SCloudaccount{} 76 if len(input.Username) == 0 { 77 return output, errors.Wrap(httperrors.ErrMissingParameter, "username") 78 } 79 if len(input.Password) == 0 { 80 return output, errors.Wrap(httperrors.ErrMissingParameter, "password") 81 } 82 output = cloudprovider.SCloudaccount{ 83 Account: input.Username, 84 Secret: input.Password, 85 } 86 return output, nil 87 } 88 89 func parseHostPort(_url string) (string, int, error) { 90 urlParse, err := url.Parse(_url) 91 if err != nil { 92 return "", 0, errors.Wrapf(err, "parse %s", _url) 93 } 94 port := func() int { 95 if len(urlParse.Port()) > 0 { 96 _port, _ := strconv.Atoi(urlParse.Port()) 97 return _port 98 } 99 return 9440 100 }() 101 return strings.TrimSuffix(urlParse.Host, fmt.Sprintf(":%d", port)), port, nil 102 } 103 104 func (self *SNutanixProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 105 host, port, err := parseHostPort(cfg.URL) 106 if err != nil { 107 return nil, errors.Wrapf(err, "parseHostPort") 108 } 109 110 client, err := nutanix.NewNutanixClient( 111 nutanix.NewNutanixClientConfig( 112 host, cfg.Account, cfg.Secret, port, 113 ).CloudproviderConfig(cfg), 114 ) 115 if err != nil { 116 return nil, err 117 } 118 return &SNutanixProvider{ 119 SBaseProvider: cloudprovider.NewBaseProvider(self), 120 client: client, 121 }, nil 122 } 123 124 func (self *SNutanixProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 125 host, port, err := parseHostPort(info.Url) 126 if err != nil { 127 return nil, err 128 } 129 130 return map[string]string{ 131 "NUTANIX_HOST": host, 132 "NUTANIX_PORT": fmt.Sprintf("%d", port), 133 "NUTANIX_USERNAME": info.Account, 134 "NUTANIX_PASSWORD": info.Secret, 135 }, nil 136 } 137 138 func init() { 139 factory := SNutanixProviderFactory{} 140 cloudprovider.RegisterFactory(&factory) 141 } 142 143 type SNutanixProvider struct { 144 cloudprovider.SBaseProvider 145 client *nutanix.SNutanixClient 146 } 147 148 func (self *SNutanixProvider) GetSysInfo() (jsonutils.JSONObject, error) { 149 return jsonutils.NewDict(), nil 150 } 151 152 func (self *SNutanixProvider) GetVersion() string { 153 return nutanix.NUTANIX_VERSION_V2 154 } 155 156 func (self *SNutanixProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 157 return self.client.GetSubAccounts() 158 } 159 160 func (self *SNutanixProvider) GetAccountId() string { 161 return self.client.GetAccountId() 162 } 163 164 func (self *SNutanixProvider) GetIRegions() []cloudprovider.ICloudRegion { 165 return self.client.GetIRegions() 166 } 167 168 func (self *SNutanixProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) { 169 regions := self.GetIRegions() 170 for i := range regions { 171 if regions[i].GetGlobalId() == id { 172 return regions[i], nil 173 } 174 } 175 return nil, cloudprovider.ErrNotFound 176 } 177 178 func (self *SNutanixProvider) GetBalance() (float64, string, error) { 179 return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported 180 } 181 182 func (self *SNutanixProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 183 return []cloudprovider.ICloudProject{}, nil 184 //return self.client.GetIProjects() 185 } 186 187 func (self *SNutanixProvider) GetStorageClasses(regionId string) []string { 188 return nil 189 } 190 191 func (self *SNutanixProvider) GetBucketCannedAcls(regionId string) []string { 192 return nil 193 } 194 195 func (self *SNutanixProvider) GetObjectCannedAcls(regionId string) []string { 196 return nil 197 } 198 199 func (self *SNutanixProvider) GetCapabilities() []string { 200 return self.client.GetCapabilities() 201 }