yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/app_service_plan.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/pkg/errors"
    22  )
    23  
    24  type SAppServicePlan struct {
    25  	AzureTags
    26  	region *SRegion
    27  
    28  	Properties SAppServicePlanProperties
    29  	ID         string
    30  	Kind       string
    31  	Location   string
    32  	Name       string
    33  	Type       string
    34  	Sku        SSkuDescription
    35  }
    36  
    37  type SAppServicePlanProperties struct {
    38  	FreeOfferExpirationTime   string
    39  	GeoRegion                 string
    40  	HostingEnvironmentProfile SHostingEnvironmentProfile
    41  	HyperV                    bool
    42  	IsSpot                    bool
    43  	MaximumElasticWorkerCount int
    44  	MaximumNumberOfWorkers    int
    45  	NumberOfSites             int
    46  	PerSiteScaling            bool
    47  	// 应用服务环境的预配状态
    48  	ProvisioningState  string
    49  	Reserved           bool
    50  	ResourceGroup      string
    51  	SpotExpirationTime string
    52  	// Creating Pending Ready
    53  	Status             string
    54  	Subscription       string
    55  	TargetWorkerCount  int
    56  	TargetWorkerSizeId int
    57  	WorkerTierName     string
    58  }
    59  
    60  type SHostingEnvironmentProfile struct {
    61  	ID   string
    62  	Name string
    63  	Type string
    64  }
    65  
    66  type SSkuDescription struct {
    67  	Capabilities []SCapability
    68  	Capacity     int
    69  	Family       string
    70  	locations    []string
    71  	Name         string
    72  	Size         string
    73  	SkuCapacity  SSkuCapacity
    74  	Tier         string
    75  }
    76  
    77  type SCapability struct {
    78  	Name   string
    79  	Reason string
    80  	Value  string
    81  }
    82  
    83  type SSkuCapacity struct {
    84  	Default   int
    85  	Maximum   int
    86  	Minimum   int
    87  	ScaleType string
    88  }
    89  
    90  func (r *SRegion) GetAppServicePlanWithCache(id string) (*SAppServicePlan, error) {
    91  	if r.appServicePlans == nil {
    92  		plans, err := r.GetAppServicePlans()
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		appServicePlans := make(map[string]*SAppServicePlan, len(plans))
    97  		for i := range plans {
    98  			appServicePlans[plans[i].ID] = &plans[i]
    99  		}
   100  		r.appServicePlans = appServicePlans
   101  	}
   102  	if ret, ok := r.appServicePlans[id]; ok {
   103  		return ret, nil
   104  	}
   105  	return r.GetAppServicePlan(id)
   106  }
   107  
   108  func (r *SRegion) GetAppServicePlans() ([]SAppServicePlan, error) {
   109  	result := []SAppServicePlan{}
   110  	resource := "Microsoft.Web/serverfarms"
   111  	err := r.list(resource, url.Values{"api-version": []string{"2019-08-01"}}, &result)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	for i := range result {
   116  		result[i].region = r
   117  	}
   118  	return result, nil
   119  }
   120  
   121  func (r *SRegion) GetAppServicePlan(planId string) (*SAppServicePlan, error) {
   122  	asp := &SAppServicePlan{region: r}
   123  	params := url.Values{"api-version": []string{"2019-08-01"}}
   124  	return asp, r.get(planId, params, asp)
   125  }
   126  
   127  type SServerFarmSku struct {
   128  	ResourceType string
   129  	Sku          SSkuSpec
   130  	Capacity     SSkuCapacity
   131  }
   132  
   133  type SSkuSpec struct {
   134  	Name string
   135  	Tier string
   136  }
   137  
   138  func (asp *SAppServicePlan) GetSkus() ([]SServerFarmSku, error) {
   139  	result := []SServerFarmSku{}
   140  	resource := fmt.Sprintf("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/serverfarms/%s/skus", asp.region.client._subscriptionId(), asp.Properties.ResourceGroup, asp.Name)
   141  	err := asp.region.client.list(resource, url.Values{"api-version": []string{"2019-08-01"}}, &result)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	return result, nil
   146  }
   147  
   148  type SAppServicePlanVnet struct {
   149  	Id         string
   150  	Name       string
   151  	Properties SASPVnetProperties
   152  }
   153  
   154  type SASPVnetProperties struct {
   155  	VnetResourceId string
   156  }
   157  
   158  func (asp *SAppServicePlan) GetNetworks() ([]SNetwork, error) {
   159  	vnets, err := asp.GetVnets()
   160  	if err != nil {
   161  		return nil, errors.Wrap(err, "unable to get Vnets")
   162  	}
   163  	networks := make([]SNetwork, 0, len(vnets))
   164  	for i := range vnets {
   165  		network, err := asp.region.GetNetwork(vnets[i].Properties.VnetResourceId)
   166  		if err != nil {
   167  			return nil, errors.Wrap(err, "unable to get Network")
   168  		}
   169  		networks = append(networks, *network)
   170  	}
   171  	return networks, nil
   172  }
   173  
   174  func (asp *SAppServicePlan) GetVnets() ([]SAppServicePlanVnet, error) {
   175  	result := []SAppServicePlanVnet{}
   176  	resource := fmt.Sprintf("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/serverfarms/%s/virtualNetworkConnections", asp.region.client._subscriptionId(), asp.Properties.ResourceGroup, asp.Name)
   177  	err := asp.region.client.list(resource, url.Values{"api-version": []string{"2019-08-01"}}, &result)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  	return result, nil
   182  }
   183  
   184  type SSkuInfos struct {
   185  	ResourceType string
   186  	Skus         []SResourceSku
   187  }
   188  
   189  func (r *SRegion) GetAppServiceSkus() ([]SSkuInfos, error) {
   190  	result := []SSkuInfos{}
   191  	resource := "Microsoft.Web/skus"
   192  	err := r.client.list(resource, url.Values{"api-version": []string{"2019-08-01"}}, &result)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  	return result, err
   197  }