yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/network_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 zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  	"yunion.io/x/pkg/utils"
    24  )
    25  
    26  type SNetworkService struct {
    27  	IPsec          []string `json:"IPsec"`
    28  	VRouterRoute   []string `json:"VRouterRoute"`
    29  	VipQos         []string `json:"VipQos"`
    30  	DNS            []string `json:"DNS"`
    31  	SNAT           []string `json:"SNAT"`
    32  	LoadBalancer   []string `json:"LoadBalancer"`
    33  	Userdata       []string `json:"Userdata"`
    34  	SecurityGroup  []string `json:"SecurityGroup"`
    35  	Eip            []string `json:"Eip"`
    36  	DHCP           []string `json:"DHCP"`
    37  	CentralizedDNS []string `json:"CentralizedDNS"`
    38  	HostRoute      []string `json:"HostRoute"`
    39  	PortForwarding []string `json:"PortForwarding"`
    40  }
    41  
    42  type SNetworkServiceProvider struct {
    43  	ZStackBasic
    44  	ZStackTime
    45  	Type                   string   `json:"type"`
    46  	NetworkServiceTypes    []string `json:"networkServiceTypes"`
    47  	AttachedL2NetworkUUIDs []string `json:"attachedL2NetworkUuids"`
    48  }
    49  
    50  type SNetworkServiceRef struct {
    51  	L3NetworkUUID              string `json:"l3NetworkUuid"`
    52  	NetworkServiceProviderUUID string `json:"networkServiceProviderUuid"`
    53  	NetworkServiceType         string `json:"networkServiceType"`
    54  }
    55  
    56  func (region *SRegion) GetNetworkServices() (*SNetworkService, error) {
    57  	service := &SNetworkService{}
    58  	resp, err := region.client.get("network-services/types", "", "")
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return service, resp.Unmarshal(service, "types")
    63  }
    64  
    65  func (region *SRegion) GetNetworkServiceProviders(Type string) ([]SNetworkServiceProvider, error) {
    66  	providers := []SNetworkServiceProvider{}
    67  	params := url.Values{}
    68  	if len(Type) > 0 {
    69  		params.Add("q", "type="+Type)
    70  	}
    71  	return providers, region.client.listAll("network-services/providers", params, &providers)
    72  }
    73  
    74  func (region *SRegion) GetNetworkServiceRef(l3Id string, Type string) ([]SNetworkServiceRef, error) {
    75  	refs := []SNetworkServiceRef{}
    76  	params := url.Values{}
    77  	if len(l3Id) > 0 {
    78  		params.Add("q", "l3NetworkUuid="+l3Id)
    79  	}
    80  	if len(Type) > 0 {
    81  		params.Add("q", "networkServiceType="+Type)
    82  	}
    83  	return refs, region.client.listAll("l3-networks/network-services/refs", params, &refs)
    84  }
    85  
    86  func (region *SRegion) AttachServiceForl3Network(l3Id string, services []string) error {
    87  	networkServices := map[string][]string{}
    88  	refs, err := region.GetNetworkServiceRef(l3Id, "")
    89  	if err != nil {
    90  		return err
    91  	}
    92  	currentServices := []string{}
    93  	for i := 0; i < len(refs); i++ {
    94  		currentServices = append(currentServices, refs[i].NetworkServiceType)
    95  	}
    96  	for _, service := range services {
    97  		networkServiceProviders, err := region.GetNetworkServiceProviders(service)
    98  		if err != nil || len(networkServiceProviders) == 0 {
    99  			msg := fmt.Sprintf("failed to find network services %s error: %v", service, err)
   100  			log.Errorln(msg)
   101  			return fmt.Errorf(msg)
   102  		}
   103  		attachServices := []string{}
   104  		for i := 0; i < len(networkServiceProviders[0].NetworkServiceTypes); i++ {
   105  			if !utils.IsInStringArray(networkServiceProviders[0].NetworkServiceTypes[i], currentServices) {
   106  				attachServices = append(attachServices, networkServiceProviders[0].NetworkServiceTypes[i])
   107  			}
   108  		}
   109  		if len(attachServices) > 0 {
   110  			networkServices[networkServiceProviders[0].UUID] = attachServices
   111  		}
   112  	}
   113  
   114  	if len(networkServices) == 0 {
   115  		return nil
   116  	}
   117  	params := map[string]interface{}{
   118  		"params": map[string]interface{}{
   119  			"networkServices": networkServices,
   120  		},
   121  	}
   122  	resource := fmt.Sprintf("l3-networks/%s/network-services", l3Id)
   123  	_, err = region.client.post(resource, jsonutils.Marshal(params))
   124  	if err != nil {
   125  		log.Errorf("failed to attach network services %s to l3network %s error: %v", services, l3Id, err)
   126  	}
   127  	return err
   128  }
   129  
   130  func (region *SRegion) RemoveNetworkService(l3Id string, service string) error {
   131  	return region.client.delete("l3-networks", fmt.Sprintf("%s/network-services?networkServices=%s", l3Id, service), "")
   132  }