forked from GoogleCloudPlatform/terraformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for ibm directlink and transit gateway resources added (Googl…
…eCloudPlatform#1049) Co-authored-by: Sergey Lanzman <[email protected]>
- Loading branch information
1 parent
96ddb64
commit 7e64e27
Showing
4 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2019 The Terraformer Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
"github.com/IBM/go-sdk-core/v4/core" | ||
dlProviderV2 "github.com/IBM/networking-go-sdk/directlinkproviderv2" | ||
dl "github.com/IBM/networking-go-sdk/directlinkv1" | ||
) | ||
|
||
// DLGenerator ... | ||
type DLGenerator struct { | ||
IBMService | ||
} | ||
|
||
func (g DLGenerator) createDirectLinkGatewayResources(gatewayID, gatewayName string) terraformutils.Resource { | ||
resources := terraformutils.NewSimpleResource( | ||
gatewayID, | ||
gatewayName, | ||
"ibm_dl_gateway", | ||
"ibm", | ||
[]string{}) | ||
return resources | ||
} | ||
|
||
func (g DLGenerator) createDirectLinkVirtualConnectionResources(gatewayID, connectionID, connectionName string, dependsOn []string) terraformutils.Resource { | ||
resources := terraformutils.NewResource( | ||
fmt.Sprintf("%s/%s", gatewayID, connectionID), | ||
connectionName, | ||
"ibm_dl_virtual_connection", | ||
"ibm", | ||
map[string]string{}, | ||
[]string{}, | ||
map[string]interface{}{ | ||
"depends_on": dependsOn, | ||
}) | ||
return resources | ||
} | ||
|
||
func (g DLGenerator) createDirectLinkProviderGatewayResources(providerGatewayID, providerGatewayName string) terraformutils.Resource { | ||
resources := terraformutils.NewSimpleResource( | ||
providerGatewayID, | ||
providerGatewayName, | ||
"ibm_dl_provider_gateway", | ||
"ibm", | ||
[]string{}) | ||
return resources | ||
} | ||
|
||
// InitResources ... | ||
func (g *DLGenerator) InitResources() error { | ||
apiKey := os.Getenv("IC_API_KEY") | ||
if apiKey == "" { | ||
return fmt.Errorf("no API key set") | ||
} | ||
dlURL := "https://directlink.cloud.ibm.com/v1" | ||
directlinkOptions := &dl.DirectLinkV1Options{ | ||
URL: envFallBack([]string{"IBMCLOUD_DL_API_ENDPOINT"}, dlURL), | ||
Authenticator: &core.IamAuthenticator{ | ||
ApiKey: apiKey, | ||
}, | ||
Version: CreateVersionDate(), | ||
} | ||
dlclient, err := dl.NewDirectLinkV1(directlinkOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
listGatewaysOptions := &dl.ListGatewaysOptions{} | ||
gateways, response, err := dlclient.ListGateways(listGatewaysOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Fetching Direct Link Gateways %s\n%s", err, response) | ||
} | ||
for _, gateway := range gateways.Gateways { | ||
g.Resources = append(g.Resources, g.createDirectLinkGatewayResources(*gateway.ID, *gateway.Name)) | ||
var dependsOn []string | ||
dependsOn = append(dependsOn, | ||
"ibm_dl_gateway."+terraformutils.TfSanitize(*gateway.Name)) | ||
listGatewayVirtualConnectionsOptions := &dl.ListGatewayVirtualConnectionsOptions{ | ||
GatewayID: gateway.ID, | ||
} | ||
connections, response, err := dlclient.ListGatewayVirtualConnections(listGatewayVirtualConnectionsOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Fetching Direct Link Virtual connections %s\n%s", err, response) | ||
} | ||
for _, connection := range connections.VirtualConnections { | ||
g.Resources = append(g.Resources, g.createDirectLinkVirtualConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn)) | ||
} | ||
} | ||
|
||
dlproviderURL := "https://directlink.cloud.ibm.com/provider/v2" | ||
dlproviderOptions := &dlProviderV2.DirectLinkProviderV2Options{ | ||
URL: envFallBack([]string{"IBMCLOUD_DL_PROVIDER_API_ENDPOINT"}, dlproviderURL), | ||
Authenticator: &core.IamAuthenticator{ | ||
ApiKey: apiKey, | ||
}, | ||
Version: CreateVersionDate(), | ||
} | ||
dlproviderclient, err := dlProviderV2.NewDirectLinkProviderV2(dlproviderOptions) | ||
if err != nil { | ||
return err | ||
} | ||
start := "" | ||
allrecs := []dlProviderV2.ProviderGateway{} | ||
for { | ||
listProviderGatewaysOptions := &dlProviderV2.ListProviderGatewaysOptions{} | ||
if start != "" { | ||
listProviderGatewaysOptions.Start = &start | ||
} | ||
|
||
providerGateways, resp, err := dlproviderclient.ListProviderGateways(listProviderGatewaysOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Listing Direct Link Provider Gateways %s\n%s", err, resp) | ||
} | ||
start = GetNext(providerGateways.Next) | ||
allrecs = append(allrecs, providerGateways.Gateways...) | ||
if start == "" { | ||
break | ||
} | ||
} | ||
for _, providerGateway := range allrecs { | ||
g.Resources = append(g.Resources, g.createDirectLinkProviderGatewayResources(*providerGateway.ID, *providerGateway.Name)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2019 The Terraformer Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
"github.com/IBM/go-sdk-core/v4/core" | ||
tg "github.com/IBM/networking-go-sdk/transitgatewayapisv1" | ||
) | ||
|
||
// TGGenerator ... | ||
type TGGenerator struct { | ||
IBMService | ||
} | ||
|
||
func (g TGGenerator) createTransitGatewayResources(gatewayID, gatewayName string) terraformutils.Resource { | ||
resources := terraformutils.NewSimpleResource( | ||
gatewayID, | ||
gatewayName, | ||
"ibm_tg_gateway", | ||
"ibm", | ||
[]string{}) | ||
return resources | ||
} | ||
|
||
func (g TGGenerator) createTransitGatewayConnectionResources(gatewayID, connectionID, connectionName string, dependsOn []string) terraformutils.Resource { | ||
resources := terraformutils.NewResource( | ||
fmt.Sprintf("%s/%s", gatewayID, connectionID), | ||
connectionName, | ||
"ibm_tg_connection", | ||
"ibm", | ||
map[string]string{}, | ||
[]string{}, | ||
map[string]interface{}{ | ||
"depends_on": dependsOn, | ||
}) | ||
return resources | ||
} | ||
|
||
// CreateVersionDate requires mandatory version attribute. Any date from 2019-12-13 up to the currentdate may be provided. Specify the current date to request the latest version. | ||
func CreateVersionDate() *string { | ||
version := time.Now().Format("2006-01-02") | ||
return &version | ||
} | ||
|
||
// InitResources ... | ||
func (g *TGGenerator) InitResources() error { | ||
apiKey := os.Getenv("IC_API_KEY") | ||
if apiKey == "" { | ||
return fmt.Errorf("no API key set") | ||
} | ||
tgURL := "https://transit.cloud.ibm.com/v1" | ||
transitgatewayOptions := &tg.TransitGatewayApisV1Options{ | ||
URL: envFallBack([]string{"IBMCLOUD_TG_API_ENDPOINT"}, tgURL), | ||
Authenticator: &core.IamAuthenticator{ | ||
ApiKey: apiKey, | ||
}, | ||
Version: CreateVersionDate(), | ||
} | ||
|
||
tgclient, err := tg.NewTransitGatewayApisV1(transitgatewayOptions) | ||
if err != nil { | ||
return err | ||
} | ||
start := "" | ||
allrecs := []tg.TransitGateway{} | ||
for { | ||
listTransitGatewaysOptions := &tg.ListTransitGatewaysOptions{} | ||
if start != "" { | ||
listTransitGatewaysOptions.Start = &start | ||
} | ||
|
||
gateways, resp, err := tgclient.ListTransitGateways(listTransitGatewaysOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Listing Transit Gateways %s\n%s", err, resp) | ||
} | ||
start = GetNext(gateways.Next) | ||
allrecs = append(allrecs, gateways.TransitGateways...) | ||
if start == "" { | ||
break | ||
} | ||
} | ||
for _, gateway := range allrecs { | ||
g.Resources = append(g.Resources, g.createTransitGatewayResources(*gateway.ID, *gateway.Name)) | ||
var dependsOn []string | ||
dependsOn = append(dependsOn, | ||
"ibm_tg_gateway."+terraformutils.TfSanitize(*gateway.Name)) | ||
listTransitGatewayConnectionsOptions := &tg.ListTransitGatewayConnectionsOptions{ | ||
TransitGatewayID: gateway.ID, | ||
} | ||
connections, response, err := tgclient.ListTransitGatewayConnections(listTransitGatewayConnectionsOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error Listing Transit Gateway connections %s\n%s", err, response) | ||
} | ||
for _, connection := range connections.Connections { | ||
g.Resources = append(g.Resources, g.createTransitGatewayConnectionResources(*gateway.ID, *connection.ID, *connection.Name, dependsOn)) | ||
} | ||
} | ||
return nil | ||
} |