Add public IP address
This commit is contained in:
Родитель
138d4dbf77
Коммит
365d0e78b1
1
go.mod
1
go.mod
|
@ -61,6 +61,7 @@ replace (
|
|||
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.1.0
|
||||
github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
|
||||
github.com/hashicorp/go-retryablehttp => github.com/hashicorp/go-retryablehttp v0.7.7
|
||||
github.com/microsoft/moc => github.com/microsoft/moc v0.23.13-0.20241105033401-9cb75ab19c19
|
||||
github.com/miekg/dns => github.com/miekg/dns v1.1.25
|
||||
github.com/nats-io/nkeys => github.com/nats-io/nkeys v0.4.6
|
||||
golang.org/x/crypto => golang.org/x/crypto v0.26.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -73,8 +73,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/microsoft/moc v0.23.11 h1:aA6xQRjemjB+rFjECNoGKSGCdqBkHPO3Qwiu4soPwIA=
|
||||
github.com/microsoft/moc v0.23.11/go.mod h1:TE8K8CXgXF+VqnILHqSf93qUamy2BJgWCkuiNBeARhM=
|
||||
github.com/microsoft/moc v0.23.13-0.20241105033401-9cb75ab19c19 h1:ReiiHlr+E5X6pAm+4AlT+AGicgcRLBjUd3QZOdd/hKY=
|
||||
github.com/microsoft/moc v0.23.13-0.20241105033401-9cb75ab19c19/go.mod h1:TE8K8CXgXF+VqnILHqSf93qUamy2BJgWCkuiNBeARhM=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
|
|
|
@ -79,3 +79,13 @@ func GetNetworkSecurityGroupClient(serverAddress *string, authorizer auth.Author
|
|||
|
||||
return network_pb.NewNetworkSecurityGroupAgentClient(conn), nil
|
||||
}
|
||||
|
||||
// GetPublicIPAddressAgentClient returns the PublicIPAddressClient client to communicate with the wssd agent
|
||||
func GetPublicIPAddressAgentClient(serverAddress *string, authorizer auth.Authorizer) (network_pb.PublicIPAddressAgentClient, error) {
|
||||
conn, err := getClientConnection(serverAddress, authorizer)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get PublicIPAddressAgentClient. Failed to dial: %v", err)
|
||||
}
|
||||
|
||||
return network_pb.NewPublicIPAddressAgentClient(conn), nil
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ func getRpcPlacementGroup(s *compute.PlacementGroup, group string) (*wssdcloudco
|
|||
}
|
||||
|
||||
placementGroup.Zones = &wssdcloudproto.ZoneConfiguration{
|
||||
Zones: []*wssdcloudproto.ZoneReference{},
|
||||
Zones: []*wssdcloudproto.ZoneReference{},
|
||||
StrictPlacement: false,
|
||||
}
|
||||
|
||||
|
@ -113,16 +113,16 @@ func getWssdPlacementGroup(s *wssdcloudcompute.PlacementGroup) (*compute.Placeme
|
|||
pgStrictPlacement := false
|
||||
|
||||
if s.Zones != nil {
|
||||
if s.Zones.Zones != nil {
|
||||
for _, zn := range s.Zones.Zones {
|
||||
pgZone = append(pgZone, zn.Name)
|
||||
}
|
||||
if s.Zones.Zones != nil {
|
||||
for _, zn := range s.Zones.Zones {
|
||||
pgZone = append(pgZone, zn.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Zones.StrictPlacement {
|
||||
pgStrictPlacement = s.Zones.StrictPlacement
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pgScope := compute.ServerScope
|
||||
if s.Scope == wssdcloudcompute.PlacementGroupScope_Zone {
|
||||
|
|
|
@ -684,7 +684,7 @@ type InterfaceIPConfigurationPropertiesFormat struct {
|
|||
// Primary - Gets whether this is a primary customer address on the network interface.
|
||||
Primary *bool `json:"primary,omitempty"`
|
||||
// PublicIPAddress - Public IP address bound to the IP configuration.
|
||||
PublicIPAddress *PublicIPAddress `json:"publicIPAddress,omitempty"`
|
||||
PublicIPAddress *SubResource `json:"publicIPAddress,omitempty"`
|
||||
// ProvisioningState - The provisioning state of the network interface IP configuration. Possible values are: 'Updating', 'Deleting', and 'Failed'.
|
||||
ProvisioningState *string `json:"provisioningState,omitempty"`
|
||||
// State - State
|
||||
|
@ -963,22 +963,25 @@ type IPTag struct {
|
|||
|
||||
// PublicIPAddressProperties public IP address properties.
|
||||
type PublicIPAddressPropertiesFormat struct {
|
||||
// IPAddress - The IP address associated with the public IP address resource.
|
||||
IPAddress *string `json:"ipAddress,omitempty"`
|
||||
// PublicIPAllocationMethod - The public IP address allocation method. Possible values include: 'Static', 'Dynamic'
|
||||
PublicIPAllocationMethod IPAllocationMethod `json:"publicIPAllocationMethod,omitempty"`
|
||||
// PublicIPAddressVersion - The public IP address version. Possible values include: 'IPv4', 'IPv6'
|
||||
PublicIPAddressVersion IPVersion `json:"publicIPAddressVersion,omitempty"`
|
||||
// IPConfiguration - READ-ONLY; The IP configuration associated with the public IP address.
|
||||
IPConfiguration *IPConfiguration `json:"ipConfiguration,omitempty"`
|
||||
|
||||
// No support now
|
||||
// DNSSettings - The FQDN of the DNS record associated with the public IP address.
|
||||
DNSSettings *PublicIPAddressDNSSettings `json:"dnsSettings,omitempty"`
|
||||
// DNSSettings *PublicIPAddressDNSSettings `json:"dnsSettings,omitempty"`
|
||||
// IPTags - The list of tags associated with the public IP address.
|
||||
IPTags *[]IPTag `json:"ipTags,omitempty"`
|
||||
// IPAddress - The IP address associated with the public IP address resource.
|
||||
IPAddress *string `json:"ipAddress,omitempty"`
|
||||
// IPTags *[]IPTag `json:"ipTags,omitempty"`
|
||||
// PublicIPPrefix - The Public IP Prefix this Public IP Address should be allocated from.
|
||||
PublicIPPrefix *SubResource `json:"publicIPPrefix,omitempty"`
|
||||
// PublicIPPrefix *SubResource `json:"publicIPPrefix,omitempty"`
|
||||
|
||||
// IdleTimeoutInMinutes - The idle timeout of the public IP address.
|
||||
IdleTimeoutInMinutes *int32 `json:"idleTimeoutInMinutes,omitempty"`
|
||||
IdleTimeoutInMinutes *uint32 `json:"idleTimeoutInMinutes,omitempty"`
|
||||
// ProvisioningState - The provisioning state of the PublicIP resource. Possible values are: 'Updating', 'Deleting', and 'Failed'.
|
||||
ProvisioningState *string `json:"provisioningState,omitempty"`
|
||||
// State - State
|
||||
|
@ -992,14 +995,19 @@ type PublicIPAddress struct {
|
|||
*PublicIPAddressPropertiesFormat `json:"properties,omitempty"`
|
||||
// Etag - A unique read-only string that changes whenever the resource is updated.
|
||||
Etag *string `json:"etag,omitempty"`
|
||||
|
||||
// No support now
|
||||
// Zones - A list of availability zones denoting the IP allocated for the resource needs to come from.
|
||||
Zones *[]string `json:"zones,omitempty"`
|
||||
// Zones *[]string `json:"zones,omitempty"`
|
||||
|
||||
// ID - Resource ID.
|
||||
ID *string `json:"id,omitempty"`
|
||||
// Name - READ-ONLY; Resource name.
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Type - READ-ONLY; Resource type.
|
||||
Type *string `json:"type,omitempty"`
|
||||
// Version
|
||||
Version *string `json:"version,omitempty"`
|
||||
// Location - Resource location.
|
||||
Location *string `json:"location,omitempty"`
|
||||
// Tags - Resource tags.
|
||||
|
|
|
@ -136,6 +136,14 @@ func getWssdNetworkInterfaceIPConfig(ipConfig *network.InterfaceIPConfiguration,
|
|||
},
|
||||
}
|
||||
}
|
||||
if ipConfig.PublicIPAddress != nil {
|
||||
wssdipconfig.PublicIPAddressRef = &wssdcommonproto.PublicIPAddressReference{
|
||||
ResourceRef: &wssdcommonproto.ResourceReference{
|
||||
Name: *ipConfig.PublicIPAddress.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
ipAllocationMethodSdkToProtobuf(ipConfig, wssdipconfig)
|
||||
|
||||
if ipConfig.LoadBalancerBackendAddressPools != nil {
|
||||
|
@ -202,6 +210,12 @@ func getNetworkIpConfig(wssdcloudipconfig *wssdcloudnetwork.IpConfiguration) *ne
|
|||
}
|
||||
}
|
||||
|
||||
if wssdcloudipconfig.PublicIPAddressRef != nil {
|
||||
ipconfig.InterfaceIPConfigurationPropertiesFormat.PublicIPAddress = &network.SubResource{
|
||||
ID: &wssdcloudipconfig.PublicIPAddressRef.ResourceRef.Name,
|
||||
}
|
||||
}
|
||||
|
||||
ipAllocationMethodProtobufToSdk(wssdcloudipconfig, ipconfig)
|
||||
|
||||
var addresspools []network.BackendAddressPool
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the Apache v2.0 License.
|
||||
|
||||
package publicipaddress
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/microsoft/moc-sdk-for-go/services/network"
|
||||
"github.com/microsoft/moc/pkg/auth"
|
||||
)
|
||||
|
||||
// Service interface
|
||||
type Service interface {
|
||||
Get(context.Context, string, string) (*[]network.PublicIPAddress, error)
|
||||
CreateOrUpdate(context.Context, string, string, *network.PublicIPAddress) (*network.PublicIPAddress, error)
|
||||
Delete(context.Context, string, string) error
|
||||
Precheck(ctx context.Context, group string, pips []*network.PublicIPAddress) (bool, error)
|
||||
}
|
||||
|
||||
// PublicIPAddressAgentClient structure
|
||||
type PublicIPAddressAgentClient struct {
|
||||
network.BaseClient
|
||||
internal Service
|
||||
}
|
||||
|
||||
// PublicIPAddressAgentClient method returns new client
|
||||
func NewPublicIPAddressClient(cloudFQDN string, authorizer auth.Authorizer) (*PublicIPAddressAgentClient, error) {
|
||||
c, err := newPublicIPAddressAgentClient(cloudFQDN, authorizer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PublicIPAddressAgentClient{internal: c}, nil
|
||||
}
|
||||
|
||||
// Get methods invokes the client Get method
|
||||
func (c *PublicIPAddressAgentClient) Get(ctx context.Context, group, name string) (*[]network.PublicIPAddress, error) {
|
||||
return c.internal.Get(ctx, group, name)
|
||||
}
|
||||
|
||||
// Ensure methods invokes create or update on the client
|
||||
func (c *PublicIPAddressAgentClient) CreateOrUpdate(ctx context.Context, group, name string, pip *network.PublicIPAddress) (*network.PublicIPAddress, error) {
|
||||
return c.internal.CreateOrUpdate(ctx, group, name, pip)
|
||||
}
|
||||
|
||||
// Delete methods invokes delete of the network resource
|
||||
func (c *PublicIPAddressAgentClient) Delete(ctx context.Context, group, name string) error {
|
||||
return c.internal.Delete(ctx, group, name)
|
||||
}
|
||||
|
||||
// Prechecks whether the system is able to create specified resources.
|
||||
// Returns true if it is possible; or false with reason in error message if not.
|
||||
func (c *PublicIPAddressAgentClient) Precheck(ctx context.Context, group string, pip []*network.PublicIPAddress) (bool, error) {
|
||||
return c.internal.Precheck(ctx, group, pip)
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the Apache v2.0 License.
|
||||
package publicipaddress
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/microsoft/moc-sdk-for-go/services/network"
|
||||
"github.com/microsoft/moc/pkg/errors"
|
||||
"github.com/microsoft/moc/pkg/status"
|
||||
"github.com/microsoft/moc/pkg/tags"
|
||||
wssdcloudnetwork "github.com/microsoft/moc/rpc/cloudagent/network"
|
||||
wssdcommonproto "github.com/microsoft/moc/rpc/common"
|
||||
)
|
||||
|
||||
// GetWssdPublicIPAddress converts our internal representation of a PublicIPAddress (network.PublicIPAddress) to the cloud public IP address protobuf used by wssdcloudagent (wssdnetwork.PublicIPAddress)
|
||||
func getWssdPublicIPAddress(networkPip *network.PublicIPAddress, group string) (wssdCloudPip *wssdcloudnetwork.PublicIPAddress, err error) {
|
||||
|
||||
// // if networkPip == nil || networkPip.PublicIPAddressPropertiesFormat == nil {
|
||||
// // return nil, errors.Wrapf(errors.InvalidConfiguration, "Missing public IP address Properties")
|
||||
// // }
|
||||
|
||||
if len(group) == 0 {
|
||||
return nil, errors.Wrapf(errors.InvalidInput, "Group not specified")
|
||||
}
|
||||
|
||||
if networkPip.Name == nil {
|
||||
return nil, errors.Wrapf(errors.InvalidConfiguration, "Missing Name for public IP Address")
|
||||
}
|
||||
|
||||
wssdCloudPip = &wssdcloudnetwork.PublicIPAddress{
|
||||
Name: *networkPip.Name,
|
||||
GroupName: group,
|
||||
//IdleTimeoutInMinutes: uint32(*networkPip.IdleTimeoutInMinutes),
|
||||
//IpAddress: *networkPip.IPAddress,
|
||||
//Allocation: ipAllocationMethodSdkToProtobuf(networkPip.PublicIPAllocationMethod),
|
||||
//IpVersion: ipVersionSdkToProtobuf(networkPip.PublicIPAddressVersion),
|
||||
//DomainNameLabel: *networkPip.DNSSettings.DomainNameLabel,
|
||||
//ReverseFqdn: *networkPip.DNSSettings.ReverseFqdn,
|
||||
}
|
||||
|
||||
if networkPip.Location != nil {
|
||||
wssdCloudPip.LocationName = *networkPip.Location
|
||||
}
|
||||
|
||||
if networkPip.PublicIPAddressPropertiesFormat == nil {
|
||||
return wssdCloudPip, nil
|
||||
}
|
||||
|
||||
if networkPip.IdleTimeoutInMinutes != nil {
|
||||
wssdCloudPip.IdleTimeoutInMinutes = uint32(*networkPip.IdleTimeoutInMinutes)
|
||||
}
|
||||
|
||||
switch ipAllocationMethodSdkToProtobuf(networkPip.PublicIPAllocationMethod) {
|
||||
case wssdcommonproto.IPAllocationMethod_Static:
|
||||
if networkPip.IPAddress != nil {
|
||||
wssdCloudPip.IpAddress = *networkPip.IPAddress
|
||||
wssdCloudPip.Allocation = wssdcommonproto.IPAllocationMethod_Static
|
||||
parsedIP := net.ParseIP(*networkPip.IPAddress)
|
||||
switch ipVersionSdkToProtobuf(networkPip.PublicIPAddressVersion) {
|
||||
case wssdcommonproto.IPVersion_IPv4:
|
||||
if parsedIP != nil && parsedIP.To4() != nil {
|
||||
wssdCloudPip.IpVersion = wssdcommonproto.IPVersion_IPv4
|
||||
} else {
|
||||
return nil, errors.Wrapf(errors.InvalidInput, "Public IP address is not in IPv4 format")
|
||||
}
|
||||
case wssdcommonproto.IPVersion_IPv6:
|
||||
if parsedIP != nil && parsedIP.To16() != nil {
|
||||
wssdCloudPip.IpVersion = wssdcommonproto.IPVersion_IPv6
|
||||
} else {
|
||||
return nil, errors.Wrapf(errors.InvalidInput, "Public IP address is not in IPv6 format")
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
return nil, errors.Wrapf(errors.InvalidInput, "Missing public IP address with static allocation")
|
||||
}
|
||||
case wssdcommonproto.IPAllocationMethod_Dynamic:
|
||||
wssdCloudPip.Allocation = wssdcommonproto.IPAllocationMethod_Dynamic
|
||||
wssdCloudPip.IpVersion = ipVersionSdkToProtobuf(networkPip.PublicIPAddressVersion)
|
||||
}
|
||||
|
||||
// No support for now
|
||||
// if networkPip.PublicIPAddressPropertiesFormat.DNSSettings == nil {
|
||||
// return wssdCloudPip, nil
|
||||
// }
|
||||
|
||||
// if networkPip.DNSSettings.DomainNameLabel != nil {
|
||||
// wssdCloudPip.DomainNameLabel = *networkPip.DNSSettings.DomainNameLabel
|
||||
// }
|
||||
|
||||
// if networkPip.DNSSettings.ReverseFqdn != nil {
|
||||
// wssdCloudPip.ReverseFqdn = *networkPip.DNSSettings.ReverseFqdn
|
||||
// }
|
||||
|
||||
if networkPip.Tags != nil {
|
||||
wssdCloudPip.Tags = tags.MapToProto(networkPip.Tags)
|
||||
}
|
||||
|
||||
return wssdCloudPip, nil
|
||||
}
|
||||
|
||||
// GetPublicIPAddress converts the cloud public IP address protobuf returned from wssdcloudagent (wssdcloudnetwork.PublicIPAddress) to our internal representation of a public IP address (network.PublicIPAddress)
|
||||
func getPublicIPAddress(wssdPip *wssdcloudnetwork.PublicIPAddress) (networkPip *network.PublicIPAddress, err error) {
|
||||
|
||||
networkPip = &network.PublicIPAddress{
|
||||
Name: &wssdPip.Name,
|
||||
Location: &wssdPip.LocationName,
|
||||
ID: &wssdPip.Id,
|
||||
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
|
||||
Statuses: status.GetStatuses(wssdPip.GetStatus()),
|
||||
IdleTimeoutInMinutes: &wssdPip.IdleTimeoutInMinutes,
|
||||
IPAddress: &wssdPip.IpAddress,
|
||||
PublicIPAddressVersion: ipVersionProtobufToSdk(wssdPip.IpVersion),
|
||||
PublicIPAllocationMethod: ipAllocationMethodProtobufToSdk(wssdPip.Allocation),
|
||||
// No support for now
|
||||
// DNSSettings: &network.PublicIPAddressDNSSettings{
|
||||
// DomainNameLabel: &wssdPip.DomainNameLabel,
|
||||
// Fqdn: &wssdPip.Fqdn,
|
||||
// ReverseFqdn: &wssdPip.ReverseFqdn,
|
||||
// },
|
||||
},
|
||||
}
|
||||
|
||||
if wssdPip.Tags != nil {
|
||||
networkPip.Tags = tags.ProtoToMap(wssdPip.Tags)
|
||||
}
|
||||
|
||||
return networkPip, nil
|
||||
}
|
||||
|
||||
func ipAllocationMethodProtobufToSdk(allocation wssdcommonproto.IPAllocationMethod) network.IPAllocationMethod {
|
||||
switch allocation {
|
||||
case wssdcommonproto.IPAllocationMethod_Static:
|
||||
return network.Static
|
||||
case wssdcommonproto.IPAllocationMethod_Dynamic:
|
||||
return network.Dynamic
|
||||
}
|
||||
return network.Dynamic
|
||||
}
|
||||
|
||||
func ipAllocationMethodSdkToProtobuf(allocation network.IPAllocationMethod) wssdcommonproto.IPAllocationMethod {
|
||||
switch allocation {
|
||||
case network.Static:
|
||||
return wssdcommonproto.IPAllocationMethod_Static
|
||||
case network.Dynamic:
|
||||
return wssdcommonproto.IPAllocationMethod_Dynamic
|
||||
}
|
||||
return wssdcommonproto.IPAllocationMethod_Dynamic
|
||||
}
|
||||
|
||||
func ipVersionProtobufToSdk(ipversion wssdcommonproto.IPVersion) network.IPVersion {
|
||||
switch ipversion {
|
||||
case wssdcommonproto.IPVersion_IPv4:
|
||||
return network.IPv4
|
||||
case wssdcommonproto.IPVersion_IPv6:
|
||||
return network.IPv6
|
||||
}
|
||||
return network.IPv4
|
||||
}
|
||||
|
||||
func ipVersionSdkToProtobuf(ipversion network.IPVersion) wssdcommonproto.IPVersion {
|
||||
switch ipversion {
|
||||
case network.IPv4:
|
||||
return wssdcommonproto.IPVersion_IPv4
|
||||
case network.IPv6:
|
||||
return wssdcommonproto.IPVersion_IPv6
|
||||
}
|
||||
return wssdcommonproto.IPVersion_IPv4
|
||||
}
|
|
@ -0,0 +1,294 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the Apache v2.0 License.
|
||||
|
||||
package publicipaddress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
wssdcloudclient "github.com/microsoft/moc-sdk-for-go/pkg/client"
|
||||
"github.com/microsoft/moc-sdk-for-go/services/network"
|
||||
"github.com/microsoft/moc/pkg/auth"
|
||||
"github.com/microsoft/moc/pkg/errors"
|
||||
wssdcloudnetwork "github.com/microsoft/moc/rpc/cloudagent/network"
|
||||
wssdcloudcommon "github.com/microsoft/moc/rpc/common"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
wssdcloudnetwork.PublicIPAddressAgentClient
|
||||
}
|
||||
|
||||
// newClient - creates a client session with the backend wssdcloud agent
|
||||
func newPublicIPAddressAgentClient(subID string, authorizer auth.Authorizer) (*client, error) {
|
||||
c, err := wssdcloudclient.GetPublicIPAddressAgentClient(&subID, authorizer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &client{c}, nil
|
||||
}
|
||||
|
||||
// Get a public IP address by name. If name is nil, get all public IP addresses
|
||||
func (c *client) Get(ctx context.Context, group, name string) (*[]network.PublicIPAddress, error) {
|
||||
|
||||
request, err := c.getPublicIPAddressRequestByName(wssdcloudcommon.Operation_GET, group, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := c.PublicIPAddressAgentClient.Invoke(ctx, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pips, err := c.getPublicIPAddressesFromResponse(response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pips, nil
|
||||
|
||||
}
|
||||
|
||||
// CreateOrUpdate creates a public IP address if it does not exist, or updates an existing public IP address
|
||||
func (c *client) CreateOrUpdate(ctx context.Context, group, name string, inputPip *network.PublicIPAddress) (*network.PublicIPAddress, error) {
|
||||
|
||||
err := ValidateInputs(ctx, group, name, inputPip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request, err := c.getPublicIPAddressRequest(wssdcloudcommon.Operation_POST, group, name, inputPip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := c.PublicIPAddressAgentClient.Invoke(ctx, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pips, err := c.getPublicIPAddressesFromResponse(response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &(*pips)[0], nil
|
||||
}
|
||||
|
||||
// Delete a public IP address
|
||||
func (c *client) Delete(ctx context.Context, group, name string) error {
|
||||
pips, err := c.Get(ctx, group, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(*pips) == 0 {
|
||||
return fmt.Errorf("Public IP address [%s] not found", name)
|
||||
}
|
||||
|
||||
request, err := c.getPublicIPAddressRequest(wssdcloudcommon.Operation_DELETE, group, name, &(*pips)[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.PublicIPAddressAgentClient.Invoke(ctx, request)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *client) Precheck(ctx context.Context, group string, pips []*network.PublicIPAddress) (bool, error) {
|
||||
request, err := getPublicIPAddressPrecheckRequest(group, pips)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
response, err := c.PublicIPAddressAgentClient.Precheck(ctx, request)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return getPublicIPAddressPrecheckResponse(response)
|
||||
}
|
||||
|
||||
func getPublicIPAddressPrecheckRequest(group string, publicIPAddresses []*network.PublicIPAddress) (*wssdcloudnetwork.PublicIPAddressPrecheckRequest, error) {
|
||||
request := &wssdcloudnetwork.PublicIPAddressPrecheckRequest{}
|
||||
|
||||
protoPips := make([]*wssdcloudnetwork.PublicIPAddress, 0, len(publicIPAddresses))
|
||||
|
||||
for _, pip := range publicIPAddresses {
|
||||
// can public IP address ever be nil here? what would be the meaning of that?
|
||||
if pip != nil {
|
||||
protoPip, err := getWssdPublicIPAddress(pip, group)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to convert PublicIPAddress to Protobuf representation")
|
||||
}
|
||||
protoPips = append(protoPips, protoPip)
|
||||
}
|
||||
}
|
||||
|
||||
request.PublicIPAddresses = protoPips
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func getPublicIPAddressPrecheckResponse(response *wssdcloudnetwork.PublicIPAddressPrecheckResponse) (bool, error) {
|
||||
result := response.GetResult().GetValue()
|
||||
if !result {
|
||||
return result, errors.New(response.GetError())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Validate inputs
|
||||
func ValidateInputs(ctx context.Context, group, name string, inputPip *network.PublicIPAddress) error {
|
||||
|
||||
if len(group) == 0 {
|
||||
return errors.Wrapf(errors.InvalidInput, "MOC Group not specified")
|
||||
}
|
||||
|
||||
if len(name) == 0 {
|
||||
return errors.Wrapf(errors.InvalidInput, "Public IP address name not specified")
|
||||
}
|
||||
|
||||
if inputPip == nil || inputPip.PublicIPAddressPropertiesFormat == nil {
|
||||
return errors.Wrapf(errors.InvalidConfiguration, "Missing public IP address properties")
|
||||
}
|
||||
|
||||
if inputPip.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes == nil {
|
||||
//return errors.Wrapf(errors.InvalidInput, "Idle timeout in minute needs to be between 4 and 30 minutes")
|
||||
// Set default if not set
|
||||
*inputPip.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes = 4
|
||||
} else {
|
||||
if *inputPip.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes < 4 || *inputPip.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes > 30 {
|
||||
return errors.Wrapf(errors.InvalidInput, "Idle timeout in minute needs to be between 4 and 30 minutes")
|
||||
}
|
||||
}
|
||||
|
||||
if len(inputPip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion) == 0 {
|
||||
inputPip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion = network.IPv4
|
||||
} else {
|
||||
if inputPip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion != network.IPv4 && inputPip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion != network.IPv6 {
|
||||
return errors.Wrapf(errors.InvalidInput, "Invalid IP address version for public IP address")
|
||||
}
|
||||
|
||||
if inputPip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion != network.IPv4 {
|
||||
return errors.Wrapf(errors.InvalidInput, "Public IP address for IPv4 is only supported")
|
||||
}
|
||||
}
|
||||
|
||||
if len(inputPip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod) == 0 {
|
||||
inputPip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod = network.Dynamic
|
||||
} else {
|
||||
if inputPip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod != network.Dynamic && inputPip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod != network.Static {
|
||||
return errors.Wrapf(errors.InvalidInput, "Invalid IP address allocation for public IP address")
|
||||
}
|
||||
|
||||
if inputPip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod == network.Static {
|
||||
|
||||
return errors.Wrapf(errors.InvalidInput, "Public IP address for dynamic allocation is only supported")
|
||||
// // Enable these when the static allocation is supported
|
||||
// // if *inputPip.PublicIPAddressPropertiesFormat.IPAddress == "" {
|
||||
// // return errors.Wrapf(errors.InvalidInput, "Public IP address must be specified for static allocation")
|
||||
// // }
|
||||
// // parsedIP := net.ParseIP(*inputPip.PublicIPAddressPropertiesFormat.IPAddress)
|
||||
// // if parsedIP == nil || parsedIP.To4() == nil {
|
||||
// // return errors.Wrapf(errors.InvalidInput, "Public IP address must be valid IPv4 address format")
|
||||
// // }
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *client) getPublicIPAddressRequestByName(opType wssdcloudcommon.Operation, group, name string) (*wssdcloudnetwork.PublicIPAddressRequest, error) {
|
||||
pip := network.PublicIPAddress{
|
||||
Name: &name,
|
||||
}
|
||||
return c.getPublicIPAddressRequest(opType, group, name, &pip)
|
||||
}
|
||||
|
||||
// getPublicIPAddressRequest converts our internal representation of a public IP address (network.PublicIPAddress) into a protobuf request (wssdcloudnetwork.PublicIPAddressRequest) that can be sent to wssdcloudagent
|
||||
func (c *client) getPublicIPAddressRequest(opType wssdcloudcommon.Operation, group, name string, pip *network.PublicIPAddress) (*wssdcloudnetwork.PublicIPAddressRequest, error) {
|
||||
|
||||
if pip == nil {
|
||||
return nil, errors.InvalidInput
|
||||
}
|
||||
|
||||
request := &wssdcloudnetwork.PublicIPAddressRequest{
|
||||
OperationType: opType,
|
||||
PublicIPAddresses: []*wssdcloudnetwork.PublicIPAddress{},
|
||||
}
|
||||
var err error
|
||||
|
||||
wssdCloudPip, err := getWssdPublicIPAddress(pip, group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request.PublicIPAddresses = append(request.PublicIPAddresses, wssdCloudPip)
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// GetPublicIPAddressesFromResponse converts a protobuf response from wssdcloudagent (wssdcloudnetwork.PublicIPAddressResponse) to out internal representation of a public IP address (network.PublicIPAddress)
|
||||
func (c *client) getPublicIPAddressesFromResponse(response *wssdcloudnetwork.PublicIPAddressResponse) (*[]network.PublicIPAddress, error) {
|
||||
networkPips := []network.PublicIPAddress{}
|
||||
|
||||
for _, wssdCloudPip := range response.GetPublicIPAddresses() {
|
||||
networkPip, err := getPublicIPAddress(wssdCloudPip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
networkPips = append(networkPips, *networkPip)
|
||||
}
|
||||
|
||||
return &networkPips, nil
|
||||
}
|
||||
|
||||
// GetWssdPublicIPAddress converts our internal representation of a PublicIPAddress (network.PublicIPAddress) to the cloud public IP address protobuf used by wssdcloudagent (wssdnetwork.PublicIPAddress)
|
||||
|
||||
/*func getWssdPublicIPAddress(networkPip *network.PublicIPAddress, location string) (wssdCloudPip *wssdcloudnetwork.PublicIPAddress, err error) {
|
||||
|
||||
if len(location) == 0 {
|
||||
return nil, errors.Wrapf(errors.InvalidInput, "Location not specified")
|
||||
}
|
||||
|
||||
if networkPip.Name == nil {
|
||||
return nil, errors.Wrapf(errors.InvalidConfiguration, "Missing Name for public IP Address")
|
||||
}
|
||||
|
||||
wssdCloudPip = &wssdcloudnetwork.PublicIPAddress{
|
||||
Name: *networkPip.Name,
|
||||
LocationName: location,
|
||||
IpAddress: *networkPip.IPAddress,
|
||||
//Allocation: networkPip.PublicIPAllocationMethod,
|
||||
DomainNameLabel: *networkPip.DNSSettings.DomainNameLabel,
|
||||
ReverseFqdn: *networkPip.DNSSettings.ReverseFqdn,
|
||||
}
|
||||
|
||||
if networkPip.PublicIPAllocationMethod != nil {
|
||||
wssdCloudPip.Tags = tags.MapToProto(networkPip.Tags)
|
||||
}
|
||||
|
||||
if networkPip.PublicIPAddressVersion != nil {
|
||||
wssdCloudPip.Tags = tags.MapToProto(networkPip.Tags)
|
||||
}
|
||||
|
||||
if networkPip.Tags != nil {
|
||||
wssdCloudPip.Tags = tags.MapToProto(networkPip.Tags)
|
||||
}
|
||||
|
||||
return wssdCloudPip, nil
|
||||
}
|
||||
|
||||
// GetPublicIPAddress converts the cloud public IP address protobuf returned from wssdcloudagent (wssdcloudnetwork.PublicIPAddress) to our internal representation of a public IP address (network.PublicIPAddress)
|
||||
func getPublicIPAddress(wssdPip *wssdcloudnetwork.PublicIPAddress) (networkPip *network.PublicIPAddress, err error) {
|
||||
networkPip = &network.PublicIPAddress{
|
||||
Name: &wssdPip.Name,
|
||||
Location: &wssdPip.LocationName,
|
||||
ID: &wssdPip.Id,
|
||||
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
|
||||
Statuses: status.GetStatuses(wssdPip.GetStatus()),
|
||||
},
|
||||
}
|
||||
|
||||
if wssdPip.Tags != nil {
|
||||
networkPip.Tags = tags.ProtoToMap(wssdPip.Tags)
|
||||
}
|
||||
|
||||
return networkPip, nil
|
||||
}
|
||||
*/
|
Загрузка…
Ссылка в новой задаче