yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/globalnetwork.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 google
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SGlobalNetwork struct {
    28  	GoogleTags
    29  	SResourceBase
    30  
    31  	client *SGoogleClient
    32  
    33  	CreationTimestamp     time.Time
    34  	Description           string
    35  	AutoCreateSubnetworks bool
    36  	Subnetworks           []string
    37  	RoutingConfig         map[string]string
    38  	Kind                  string
    39  }
    40  
    41  func (self *SGlobalNetwork) GetStatus() string {
    42  	return api.GLOBAL_VPC_STATUS_AVAILABLE
    43  }
    44  
    45  func (self *SGlobalNetwork) IsEmulated() bool {
    46  	return false
    47  }
    48  
    49  func (self *SGlobalNetwork) Delete() error {
    50  	return self.client.ecsDelete(self.SelfLink, nil)
    51  }
    52  
    53  func (self *SGlobalNetwork) GetCreatedAt() time.Time {
    54  	return self.CreationTimestamp
    55  }
    56  
    57  func (self *SGlobalNetwork) Refresh() error {
    58  	gvpc, err := self.client.GetGlobalNetwork(self.Id)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	return jsonutils.Update(self, gvpc)
    63  }
    64  
    65  func (cli *SGoogleClient) GetGlobalNetwork(id string) (*SGlobalNetwork, error) {
    66  	net := &SGlobalNetwork{client: cli}
    67  	return net, cli.ecsGet("global/networks", id, net)
    68  }
    69  
    70  func (self *SGoogleClient) GetICloudGlobalVpcById(id string) (cloudprovider.ICloudGlobalVpc, error) {
    71  	return self.GetGlobalNetwork(id)
    72  }
    73  
    74  func (cli *SGoogleClient) GetGlobalNetworks(maxResults int, pageToken string) ([]SGlobalNetwork, error) {
    75  	networks := []SGlobalNetwork{}
    76  	params := map[string]string{}
    77  	resource := "global/networks"
    78  	if maxResults == 0 && len(pageToken) == 0 {
    79  		err := cli.ecsListAll(resource, params, &networks)
    80  		if err != nil {
    81  			return nil, errors.Wrap(err, "ecsListAll")
    82  		}
    83  		return networks, nil
    84  	}
    85  	resp, err := cli.ecsList(resource, params)
    86  	if err != nil {
    87  		return nil, errors.Wrap(err, "ecsList")
    88  	}
    89  	if resp.Contains("items") {
    90  		err = resp.Unmarshal(&networks, "items")
    91  		if err != nil {
    92  			return nil, errors.Wrap(err, "resp.Unmarshal")
    93  		}
    94  	}
    95  	return networks, nil
    96  }
    97  
    98  func (self *SGoogleClient) CreateGlobalNetwork(name string, desc string) (*SGlobalNetwork, error) {
    99  	body := map[string]interface{}{
   100  		"name":                  name,
   101  		"description":           desc,
   102  		"autoCreateSubnetworks": false,
   103  		"mtu":                   1460,
   104  		"routingConfig": map[string]string{
   105  			"routingMode": "REGIONAL",
   106  		},
   107  	}
   108  	globalnetwork := &SGlobalNetwork{client: self}
   109  	err := self.Insert("global/networks", jsonutils.Marshal(body), globalnetwork)
   110  	if err != nil {
   111  		return nil, errors.Wrap(err, "self.Insert")
   112  	}
   113  	return globalnetwork, nil
   114  }
   115  
   116  func (self *SGoogleClient) CreateICloudGlobalVpc(opts *cloudprovider.GlobalVpcCreateOptions) (cloudprovider.ICloudGlobalVpc, error) {
   117  	gvpc, err := self.CreateGlobalNetwork(opts.NAME, opts.Desc)
   118  	if err != nil {
   119  		return nil, errors.Wrapf(err, "CreateICloudGlobalVpc")
   120  	}
   121  	return gvpc, nil
   122  }
   123  
   124  func (self *SGoogleClient) GetICloudGlobalVpcs() ([]cloudprovider.ICloudGlobalVpc, error) {
   125  	gvpcs, err := self.GetGlobalNetworks(0, "")
   126  	if err != nil {
   127  		return nil, errors.Wrapf(err, "GetGlobalNetworks")
   128  	}
   129  	ret := []cloudprovider.ICloudGlobalVpc{}
   130  	for i := range gvpcs {
   131  		gvpcs[i].client = self
   132  		ret = append(ret, &gvpcs[i])
   133  	}
   134  	return ret, nil
   135  }