github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/service_alicloud_vpc.go (about) 1 package alicloud 2 3 import ( 4 "github.com/denverdino/aliyungo/common" 5 "github.com/denverdino/aliyungo/ecs" 6 "strings" 7 ) 8 9 func (client *AliyunClient) DescribeEipAddress(allocationId string) (*ecs.EipAddressSetType, error) { 10 11 args := ecs.DescribeEipAddressesArgs{ 12 RegionId: client.Region, 13 AllocationId: allocationId, 14 } 15 16 eips, _, err := client.ecsconn.DescribeEipAddresses(&args) 17 if err != nil { 18 return nil, err 19 } 20 if len(eips) == 0 { 21 return nil, common.GetClientErrorFromString("Not found") 22 } 23 24 return &eips[0], nil 25 } 26 27 func (client *AliyunClient) DescribeNatGateway(natGatewayId string) (*ecs.NatGatewaySetType, error) { 28 29 args := &ecs.DescribeNatGatewaysArgs{ 30 RegionId: client.Region, 31 NatGatewayId: natGatewayId, 32 } 33 34 natGateways, _, err := client.vpcconn.DescribeNatGateways(args) 35 //fmt.Println("natGateways %#v", natGateways) 36 if err != nil { 37 return nil, err 38 } 39 40 if len(natGateways) == 0 { 41 return nil, common.GetClientErrorFromString("Not found") 42 } 43 44 return &natGateways[0], nil 45 } 46 47 func (client *AliyunClient) DescribeVpc(vpcId string) (*ecs.VpcSetType, error) { 48 args := ecs.DescribeVpcsArgs{ 49 RegionId: client.Region, 50 VpcId: vpcId, 51 } 52 53 vpcs, _, err := client.ecsconn.DescribeVpcs(&args) 54 if err != nil { 55 if notFoundError(err) { 56 return nil, nil 57 } 58 return nil, err 59 } 60 61 if len(vpcs) == 0 { 62 return nil, nil 63 } 64 65 return &vpcs[0], nil 66 } 67 68 func (client *AliyunClient) DescribeSnatEntry(snatTableId string, snatEntryId string) (ecs.SnatEntrySetType, error) { 69 70 var resultSnat ecs.SnatEntrySetType 71 72 args := &ecs.DescribeSnatTableEntriesArgs{ 73 RegionId: client.Region, 74 SnatTableId: snatTableId, 75 } 76 77 snatEntries, _, err := client.vpcconn.DescribeSnatTableEntries(args) 78 79 //this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error" 80 //so judge the snatEntries length priority 81 if len(snatEntries) == 0 { 82 return resultSnat, common.GetClientErrorFromString(InstanceNotfound) 83 } 84 85 if err != nil { 86 return resultSnat, err 87 } 88 89 findSnat := false 90 91 for _, snat := range snatEntries { 92 if snat.SnatEntryId == snatEntryId { 93 resultSnat = snat 94 findSnat = true 95 } 96 } 97 if !findSnat { 98 return resultSnat, common.GetClientErrorFromString(NotFindSnatEntryBySnatId) 99 } 100 101 return resultSnat, nil 102 } 103 104 func (client *AliyunClient) DescribeForwardEntry(forwardTableId string, forwardEntryId string) (ecs.ForwardTableEntrySetType, error) { 105 106 var resultFoward ecs.ForwardTableEntrySetType 107 108 args := &ecs.DescribeForwardTableEntriesArgs{ 109 RegionId: client.Region, 110 ForwardTableId: forwardTableId, 111 } 112 113 forwardEntries, _, err := client.vpcconn.DescribeForwardTableEntries(args) 114 115 //this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error" 116 //so judge the snatEntries length priority 117 if len(forwardEntries) == 0 { 118 return resultFoward, common.GetClientErrorFromString(InstanceNotfound) 119 } 120 121 findForward := false 122 123 for _, forward := range forwardEntries { 124 if forward.ForwardEntryId == forwardEntryId { 125 resultFoward = forward 126 findForward = true 127 } 128 } 129 if !findForward { 130 return resultFoward, common.GetClientErrorFromString(NotFindForwardEntryByForwardId) 131 } 132 133 if err != nil { 134 return resultFoward, err 135 } 136 137 return resultFoward, nil 138 } 139 140 // describe vswitch by param filters 141 func (client *AliyunClient) QueryVswitches(args *ecs.DescribeVSwitchesArgs) (vswitches []ecs.VSwitchSetType, err error) { 142 vsws, _, err := client.ecsconn.DescribeVSwitches(args) 143 if err != nil { 144 if notFoundError(err) { 145 return nil, nil 146 } 147 return nil, err 148 } 149 150 return vsws, nil 151 } 152 153 func (client *AliyunClient) QueryVswitchById(vpcId, vswitchId string) (vsw *ecs.VSwitchSetType, err error) { 154 args := &ecs.DescribeVSwitchesArgs{ 155 VpcId: vpcId, 156 VSwitchId: vswitchId, 157 } 158 vsws, err := client.QueryVswitches(args) 159 if err != nil { 160 return nil, err 161 } 162 163 if len(vsws) == 0 { 164 return nil, nil 165 } 166 167 return &vsws[0], nil 168 } 169 170 func (client *AliyunClient) QueryRouteTables(args *ecs.DescribeRouteTablesArgs) (routeTables []ecs.RouteTableSetType, err error) { 171 rts, _, err := client.ecsconn.DescribeRouteTables(args) 172 if err != nil { 173 return nil, err 174 } 175 176 return rts, nil 177 } 178 179 func (client *AliyunClient) QueryRouteTableById(routeTableId string) (rt *ecs.RouteTableSetType, err error) { 180 args := &ecs.DescribeRouteTablesArgs{ 181 RouteTableId: routeTableId, 182 } 183 rts, err := client.QueryRouteTables(args) 184 if err != nil { 185 return nil, err 186 } 187 188 if len(rts) == 0 { 189 return nil, &common.Error{ErrorResponse: common.ErrorResponse{Message: Notfound}} 190 } 191 192 return &rts[0], nil 193 } 194 195 func (client *AliyunClient) QueryRouteEntry(routeTableId, cidrBlock, nextHopType, nextHopId string) (rn *ecs.RouteEntrySetType, err error) { 196 rt, errs := client.QueryRouteTableById(routeTableId) 197 if errs != nil { 198 return nil, errs 199 } 200 201 for _, e := range rt.RouteEntrys.RouteEntry { 202 if strings.ToLower(string(e.DestinationCidrBlock)) == cidrBlock { 203 return &e, nil 204 } 205 } 206 return nil, GetNotFoundErrorFromString("Vpc router entry not found") 207 } 208 209 func (client *AliyunClient) GetVpcIdByVSwitchId(vswitchId string) (vpcId string, err error) { 210 211 vs, _, err := client.ecsconn.DescribeVpcs(&ecs.DescribeVpcsArgs{ 212 RegionId: client.Region, 213 }) 214 if err != nil { 215 return "", err 216 } 217 218 for _, v := range vs { 219 for _, sw := range v.VSwitchIds.VSwitchId { 220 if sw == vswitchId { 221 return v.VpcId, nil 222 } 223 } 224 } 225 226 return "", &common.Error{ErrorResponse: common.ErrorResponse{Message: Notfound}} 227 }