yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/enrollment_account.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 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/log" 23 "yunion.io/x/pkg/errors" 24 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 ) 27 28 type SEnrollmentAccountProperties struct { 29 PrincipalName string 30 OfferTypes []string 31 } 32 33 type SEnrollmentAccount struct { 34 Id string 35 Name string 36 Type string 37 Properties SEnrollmentAccountProperties 38 } 39 40 func (cli *SAzureClient) GetEnrollmentAccounts() ([]cloudprovider.SEnrollmentAccount, error) { 41 accounts := []SEnrollmentAccount{} 42 err := cli.list("providers/Microsoft.Billing/enrollmentAccounts", nil, &accounts) 43 if err != nil { 44 return nil, err 45 } 46 eas := []cloudprovider.SEnrollmentAccount{} 47 for i := range accounts { 48 ea := cloudprovider.SEnrollmentAccount{ 49 Id: accounts[i].Name, 50 Name: accounts[i].Properties.PrincipalName, 51 Type: accounts[i].Type, 52 } 53 eas = append(eas, ea) 54 } 55 return eas, nil 56 } 57 58 func (cli *SAzureClient) CreateSubscription(name string, eaId string, offerType string) error { 59 appId, err := cli.GetAppObjectId() 60 if err != nil { 61 log.Errorf("GetAppObjectId error: %v", err) 62 } 63 owners := []map[string]string{} 64 if len(appId) > 0 { 65 owners = append(owners, map[string]string{"objectId": appId}) 66 } 67 body := map[string]interface{}{ 68 "displayName": name, 69 "offerType": offerType, 70 "owners": owners, 71 } 72 resource := fmt.Sprintf("providers/Microsoft.Billing/enrollmentAccounts/%s/providers/Microsoft.Subscription/createSubscription", eaId) 73 _, err = cli.post(resource, jsonutils.Marshal(body)) 74 return err 75 } 76 77 type SServicePrincipal struct { 78 AppId string 79 ObjectId string 80 } 81 82 func (cli *SAzureClient) GetAppObjectId() (string, error) { 83 result, err := cli.ListServicePrincipal(cli.clientId) 84 if err != nil { 85 return "", errors.Wrap(err, "ListServicePrincipal") 86 } 87 if len(result) == 1 { 88 return result[0].ObjectId, nil 89 } 90 return "", nil 91 } 92 93 func (cli *SAzureClient) ListServicePrincipal(appId string) ([]SServicePrincipal, error) { 94 params := url.Values{} 95 if len(appId) > 0 { 96 params.Set("$filter", fmt.Sprintf(`appId eq '%s'`, cli.clientId)) 97 } 98 result := []SServicePrincipal{} 99 return result, cli.glist("servicePrincipals", params, &result) 100 }