mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-05-31 04:50:10 +08:00
118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
package zstack
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/log"
|
|
"yunion.io/x/pkg/utils"
|
|
)
|
|
|
|
type SNetworkService struct {
|
|
IPsec []string `json:"IPsec"`
|
|
VRouterRoute []string `json:"VRouterRoute"`
|
|
VipQos []string `json:"VipQos"`
|
|
DNS []string `json:"DNS"`
|
|
SNAT []string `json:"SNAT"`
|
|
LoadBalancer []string `json:"LoadBalancer"`
|
|
Userdata []string `json:"Userdata"`
|
|
SecurityGroup []string `json:"SecurityGroup"`
|
|
Eip []string `json:"Eip"`
|
|
DHCP []string `json:"DHCP"`
|
|
CentralizedDNS []string `json:"CentralizedDNS"`
|
|
HostRoute []string `json:"HostRoute"`
|
|
PortForwarding []string `json:"PortForwarding"`
|
|
}
|
|
|
|
type SNetworkServiceProvider struct {
|
|
ZStackBasic
|
|
ZStackTime
|
|
Type string `json:"type"`
|
|
NetworkServiceTypes []string `json:"networkServiceTypes"`
|
|
AttachedL2NetworkUUIDs []string `json:"attachedL2NetworkUuids"`
|
|
}
|
|
|
|
type SNetworkServiceRef struct {
|
|
L3NetworkUUID string `json:"l3NetworkUuid"`
|
|
NetworkServiceProviderUUID string `json:"networkServiceProviderUuid"`
|
|
NetworkServiceType string `json:"networkServiceType"`
|
|
}
|
|
|
|
func (region *SRegion) GetNetworkServices() (*SNetworkService, error) {
|
|
service := &SNetworkService{}
|
|
resp, err := region.client.get("network-services/types", "", "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return service, resp.Unmarshal(service, "types")
|
|
}
|
|
|
|
func (region *SRegion) GetNetworkServiceProviders(Type string) ([]SNetworkServiceProvider, error) {
|
|
providers := []SNetworkServiceProvider{}
|
|
params := []string{}
|
|
if len(Type) > 0 {
|
|
params = append(params, "q=type="+Type)
|
|
}
|
|
return providers, region.client.listAll("network-services/providers", params, &providers)
|
|
}
|
|
|
|
func (region *SRegion) GetNetworkServiceRef(l3Id string, Type string) ([]SNetworkServiceRef, error) {
|
|
refs := []SNetworkServiceRef{}
|
|
params := []string{}
|
|
if len(l3Id) > 0 {
|
|
params = append(params, "q=l3NetworkUuid="+l3Id)
|
|
}
|
|
if len(Type) > 0 {
|
|
params = append(params, "q=networkServiceType="+Type)
|
|
}
|
|
return refs, region.client.listAll("l3-networks/network-services/refs", params, &refs)
|
|
}
|
|
|
|
func (region *SRegion) AttachServiceForl3Network(l3Id string, services []string) error {
|
|
networkServices := map[string][]string{}
|
|
refs, err := region.GetNetworkServiceRef(l3Id, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
currentServices := []string{}
|
|
for i := 0; i < len(refs); i++ {
|
|
currentServices = append(currentServices, refs[i].NetworkServiceType)
|
|
}
|
|
for _, service := range services {
|
|
networkServiceProviders, err := region.GetNetworkServiceProviders(service)
|
|
if err != nil || len(networkServiceProviders) == 0 {
|
|
msg := fmt.Sprintf("failed to find network services %s error: %v", service, err)
|
|
log.Errorln(msg)
|
|
return fmt.Errorf(msg)
|
|
}
|
|
attachServices := []string{}
|
|
for i := 0; i < len(networkServiceProviders[0].NetworkServiceTypes); i++ {
|
|
if !utils.IsInStringArray(networkServiceProviders[0].NetworkServiceTypes[i], currentServices) {
|
|
attachServices = append(attachServices, networkServiceProviders[0].NetworkServiceTypes[i])
|
|
}
|
|
}
|
|
if len(attachServices) > 0 {
|
|
networkServices[networkServiceProviders[0].UUID] = attachServices
|
|
}
|
|
}
|
|
|
|
if len(networkServices) == 0 {
|
|
return nil
|
|
}
|
|
params := map[string]interface{}{
|
|
"params": map[string]interface{}{
|
|
"networkServices": networkServices,
|
|
},
|
|
}
|
|
resource := fmt.Sprintf("l3-networks/%s/network-services", l3Id)
|
|
_, err = region.client.post(resource, jsonutils.Marshal(params))
|
|
if err != nil {
|
|
log.Errorf("failed to attach network services %s to l3network %s error: %v", services, l3Id, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (region *SRegion) RemoveNetworkService(l3Id string, service string) error {
|
|
return region.client.delete("l3-networks", fmt.Sprintf("%s/network-services?networkServices=%s", l3Id, service), "")
|
|
}
|