package main import ( "fmt" "os" "github.com/UpCloudLtd/upcloud-go-api/upcloud" "github.com/UpCloudLtd/upcloud-go-api/upcloud/request" "github.com/UpCloudLtd/upcloud-go-api/upcloud/service" ) func getNetworkInterfaces(srv ServerConfig) []request.CreateServerInterface { var networkInterfaces []request.CreateServerInterface for _,net := range srv.Networks { if net.Access == "private" { var sourceIPFiltering upcloud.Boolean = upcloud.True if net.SourceIPFiltering == "no" { sourceIPFiltering = upcloud.False } networkInterfaces = append(networkInterfaces, request.CreateServerInterface{ IPAddresses: []request.CreateServerIPAddress{ { Family: net.Family, Address: net.IPAddress, }, }, Type: net.Access, Network: net.Network, SourceIPFiltering: sourceIPFiltering, }) } else { networkInterfaces = append(networkInterfaces, request.CreateServerInterface{ IPAddresses: []request.CreateServerIPAddress{ { Family: net.Family, }, }, Type: net.Access, // SourceIPFiltering: net.SourceIPFiltering, }) } } return networkInterfaces } func modifyIPOneServer(s *service.Service, server upcloud.Server,nettype string, target ServerConfig) error { var target_NetCfg NetworksConfig for _,it := range target.Networks { if it.Access == nettype { // upcloud.NetworkTypePrivate { target_NetCfg = it break } } if target_NetCfg.IPAddress == "" { return nil } info,err := s.GetServerDetails(&request.GetServerDetailsRequest{ UUID: server.UUID, }) if err != nil { return err } // target_MAC := "" target_Index := -1 target_pos := -1 org_IPAddress := "" for i := 0; i < len(info.Networking.Interfaces); i++ { if info.Networking.Interfaces[i].Type == target_NetCfg.Access { // fmt.Fprintf(os.Stderr, "Info: %#v\n",info.Networking.Interfaces[i]) target_pos=i target_Index=info.Networking.Interfaces[i].Index for _,ip := range info.Networking.Interfaces[i].IPAddresses { if ip.Address == target_NetCfg.IPAddress { fmt.Fprintf(os.Stderr, "Already interface in server: %s to config %#v\n", server.Hostname,target_NetCfg) return nil } org_IPAddress = ip.Address } break } target_Index=info.Networking.Interfaces[i].Index } if target_pos == -1 { target_Index +=1 } if target_Index == -1 { // } && target_MAC != "" { fmt.Fprintf(os.Stderr, "Unable to find interface in server: %s to config %#v\n", server.Hostname,target_NetCfg) return nil } err = stopOneServer(s,server) if err != nil { fmt.Fprintf(os.Stderr, "Unable to stop server: %#v\n", err) return err } if target_pos > -1 { err = s.DeleteNetworkInterface(&request.DeleteNetworkInterfaceRequest{ Index: target_Index, ServerUUID: server.UUID, }) if err != nil { fmt.Fprintf(os.Stderr, "%s: Unable to delete interface %d server %s: %#v\n",org_IPAddress, target_Index,server.Hostname,err) return err } } var ipAddress []request.CreateNetworkInterfaceIPAddress var r *request.CreateNetworkInterfaceRequest switch(target_NetCfg.Access) { case upcloud.NetworkTypeUtility: ip_addrr := request.CreateNetworkInterfaceIPAddress{ Family: target_NetCfg.Family, } ipAddress = append(ipAddress, ip_addrr) r = &request.CreateNetworkInterfaceRequest{ ServerUUID: server.UUID, Type: upcloud.NetworkTypeUtility, IPAddresses: ipAddress, } case upcloud.NetworkTypePublic: ip_addrr := request.CreateNetworkInterfaceIPAddress{ Family: target_NetCfg.Family, } ipAddress = append(ipAddress, ip_addrr) r = &request.CreateNetworkInterfaceRequest{ ServerUUID: server.UUID, Type: upcloud.NetworkTypePublic, IPAddresses: ipAddress, } case upcloud.NetworkTypePrivate: ip_addrr := request.CreateNetworkInterfaceIPAddress{ Family: target_NetCfg.Family, Address: target_NetCfg.IPAddress, } var sourceIPFiltering upcloud.Boolean = upcloud.True if target_NetCfg.SourceIPFiltering == "no" { sourceIPFiltering = upcloud.False } ipAddress = append(ipAddress, ip_addrr) r = &request.CreateNetworkInterfaceRequest{ ServerUUID: server.UUID, Type: upcloud.NetworkTypePrivate, NetworkUUID: target_NetCfg.Network, IPAddresses: ipAddress, SourceIPFiltering: sourceIPFiltering, } } newIP,err := s.CreateNetworkInterface(r) if err != nil { fmt.Fprintf(os.Stderr, "error %s: %#v\n", target_NetCfg.IPAddress, err) } else { fmt.Fprintf(os.Stderr, "IP %s installed for Private Network: %s in %s\n", target_NetCfg.IPAddress, newIP.Network, server.Hostname) } err = startOneServer(s,server) if err != nil { fmt.Fprintf(os.Stderr, "Unable to start server: %#v\n", err) return err } return nil }