github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/gce/google/network.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package google 5 6 import ( 7 "sort" 8 9 "google.golang.org/api/compute/v1" 10 11 "github.com/juju/juju/network" 12 ) 13 14 const ( 15 networkDefaultName = "default" 16 networkPathRoot = "global/networks/" 17 ) 18 19 // The different kinds of network access. 20 const ( 21 NetworkAccessOneToOneNAT = "ONE_TO_ONE_NAT" // the default 22 ) 23 24 // NetworkSpec holds all the information needed to identify and create 25 // a GCE network. 26 type NetworkSpec struct { 27 // Name is the unqualified name of the network. 28 Name string 29 // TODO(ericsnow) support a CIDR for internal IP addr range? 30 } 31 32 // Path returns the qualified name of the network. 33 func (ns *NetworkSpec) Path() string { 34 name := ns.Name 35 if name == "" { 36 name = networkDefaultName 37 } 38 return networkPathRoot + name 39 } 40 41 // newInterface builds up all the data needed by the GCE API to create 42 // a new interface connected to the network. 43 func (ns *NetworkSpec) newInterface(name string) *compute.NetworkInterface { 44 var access []*compute.AccessConfig 45 if name != "" { 46 // This interface has an internet connection. 47 access = append(access, &compute.AccessConfig{ 48 Name: name, 49 Type: NetworkAccessOneToOneNAT, 50 // NatIP (only set if using a reserved public IP) 51 }) 52 // TODO(ericsnow) Will we need to support more access configs? 53 } 54 return &compute.NetworkInterface{ 55 Network: ns.Path(), 56 AccessConfigs: access, 57 } 58 } 59 60 // firewallSpec expands a port range set in to compute.FirewallAllowed 61 // and returns a compute.Firewall for the provided name. 62 func firewallSpec(name, target string, sourceCIDRs []string, ports protocolPorts) *compute.Firewall { 63 if len(sourceCIDRs) == 0 { 64 sourceCIDRs = []string{"0.0.0.0/0"} 65 } 66 firewall := compute.Firewall{ 67 // Allowed is set below. 68 // Description is not set. 69 Name: name, 70 // Network: (defaults to global) 71 // SourceTags is not set. 72 TargetTags: []string{target}, 73 SourceRanges: sourceCIDRs, 74 } 75 76 var sortedProtocols []string 77 for protocol := range ports { 78 sortedProtocols = append(sortedProtocols, protocol) 79 } 80 sort.Strings(sortedProtocols) 81 82 for _, protocol := range sortedProtocols { 83 allowed := compute.FirewallAllowed{ 84 IPProtocol: protocol, 85 Ports: ports.portStrings(protocol), 86 } 87 firewall.Allowed = append(firewall.Allowed, &allowed) 88 } 89 return &firewall 90 } 91 92 func extractAddresses(interfaces ...*compute.NetworkInterface) []network.Address { 93 var addresses []network.Address 94 95 for _, netif := range interfaces { 96 // Add public addresses. 97 for _, accessConfig := range netif.AccessConfigs { 98 if accessConfig.NatIP == "" { 99 continue 100 } 101 address := network.Address{ 102 Value: accessConfig.NatIP, 103 Type: network.IPv4Address, 104 Scope: network.ScopePublic, 105 } 106 addresses = append(addresses, address) 107 108 } 109 110 // Add private address. 111 if netif.NetworkIP == "" { 112 continue 113 } 114 address := network.Address{ 115 Value: netif.NetworkIP, 116 Type: network.IPv4Address, 117 Scope: network.ScopeCloudLocal, 118 } 119 addresses = append(addresses, address) 120 } 121 122 return addresses 123 }