Skip to content

Commit

Permalink
CIS: Routing development
Browse files Browse the repository at this point in the history
  • Loading branch information
KumarGanesanIBM authored and hkantare committed Nov 3, 2020
1 parent bf33718 commit 2df6f64
Show file tree
Hide file tree
Showing 16 changed files with 664 additions and 17 deletions.
12 changes: 12 additions & 0 deletions examples/ibm-cis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ These types of resources are supported:
* [ CIS Edge Functions Action ](https://cloud.ibm.com/docs/terraform?topic=terraform-cis-resources#cis-edge-functions-action)
* [ CIS Edge Functions Trigger ](https://cloud.ibm.com/docs/terraform?topic=terraform-cis-resources#cis-edge-functions-trigger)
* [ CIS TLS Settings](https://cloud.ibm.com/docs/terraform?topic=terraform-cis-resources#cis-tls-settings)
* [ CIS Routing](https://cloud.ibm.com/docs/terraform?topic=terraform-cis-resources#cis-routing)

## Terraform versions

Expand Down Expand Up @@ -218,6 +219,15 @@ resource "ibm_cis_tls_settings" "tls_settings" {
}
```

`CIS Routing`
```hcl
resource "ibm_cis_routing" "routing" {
cis_id = data.ibm_cis.cis.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
smart_routing = "on"
}
```

## CIS Data Sources
`CIS Instance`
```hcl
Expand Down Expand Up @@ -275,6 +285,7 @@ data "ibm_cis_edge_functions_triggers" "test_triggers" {
- [Rate Limiting CLI](https://cloud.ibm.com/docs/cis?topic=cis-cli-plugin-cis-cli#ratelimit)
- [Edge Functions CLI](https://cloud.ibm.com/docs/cis?topic=cis-cli-plugin-cis-cli#edge-functions)
- [TLS Settings CLI](https://cloud.ibm.com/docs/cis-cli-plugin?topic=cis-cli-plugin-cis-cli#show-tls-setting)
- [Routing CLI](https://cloud.ibm.com/docs/cis-cli-plugin?topic=cis-cli-plugin-cis-cli#routing)

## Notes

Expand Down Expand Up @@ -350,6 +361,7 @@ Customise the variables in `variables.tf` to your local environment and chosen D
| tls_1_3 | The TLS Version 1.3 `on`, `off`, `zrt` setting | `string` | no |
| min_tls_version | The Minimum TLS Version setting | `string` | no |
| universal_ssl | The Universal SSL enable/disable setting | `boolean` | no |
| smart_routing | The Smart Routing enable/disable setting | `string` | no |

## Outputs

Expand Down
7 changes: 7 additions & 0 deletions examples/ibm-cis/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,10 @@ resource "ibm_cis_tls_settings" "tls_settings" {
min_tls_version = "1.2"
universal_ssl = true
}

# CIS Routing
resource "ibm_cis_routing" "routing" {
cis_id = data.ibm_cis.cis.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
smart_routing = "on"
}
4 changes: 4 additions & 0 deletions examples/ibm-cis/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ output "domain_setting" {
output "ibm_cis_tls_settings_output" {
value = ibm_cis_tls_settings.tls_settings
}

output "ibm_cis_routing_output" {
value = ibm_cis_routing.routing
}
35 changes: 31 additions & 4 deletions ibm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cisglbhealthcheckv1 "github.com/IBM/networking-go-sdk/globalloadbalancermonitorv1"
cisglbpoolv0 "github.com/IBM/networking-go-sdk/globalloadbalancerpoolsv0"
cisglbv1 "github.com/IBM/networking-go-sdk/globalloadbalancerv1"
cisroutingv1 "github.com/IBM/networking-go-sdk/routingv1"
cissslv1 "github.com/IBM/networking-go-sdk/sslcertificateapiv1"
tg "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
cisratelimitv1 "github.com/IBM/networking-go-sdk/zoneratelimitsv1"
Expand Down Expand Up @@ -199,6 +200,7 @@ type ClientSession interface {
CisEdgeFunctionClientSession() (*cisedgefunctionv1.EdgeFunctionsApiV1, error)
CisSSLClientSession() (*cissslv1.SslCertificateApiV1, error)
CisDomainSettingsClientSession() (*cisdomainsettingsv1.ZonesSettingsV1, error)
CisRoutingClientSession() (*cisroutingv1.RoutingV1, error)
}

type clientSession struct {
Expand Down Expand Up @@ -350,6 +352,10 @@ type clientSession struct {
// CIS Zone Setting service options
cisDomainSettingsErr error
cisDomainSettingsClient *cisdomainsettingsv1.ZonesSettingsV1

// CIS Routing service options
cisRoutingErr error
cisRoutingClient *cisroutingv1.RoutingV1
}

// BluemixAcccountAPI ...
Expand Down Expand Up @@ -583,6 +589,11 @@ func (sess clientSession) CisDomainSettingsClientSession() (*cisdomainsettingsv1
return sess.cisDomainSettingsClient, sess.cisDomainSettingsErr
}

// CIS Routing
func (sess clientSession) CisRoutingClientSession() (*cisroutingv1.RoutingV1, error) {
return sess.cisRoutingClient, sess.cisRoutingErr
}

// ClientSession configures and returns a fully initialized ClientSession
func (c *Config) ClientSession() (interface{}, error) {
sess, err := newSession(c)
Expand Down Expand Up @@ -644,6 +655,7 @@ func (c *Config) ClientSession() (interface{}, error) {
session.cisEdgeFunctionErr = errEmptyBluemixCredentials
session.cisSSLErr = errEmptyBluemixCredentials
session.cisDomainSettingsErr = errEmptyBluemixCredentials
session.cisRoutingErr = errEmptyBluemixCredentials

return session, nil
}
Expand Down Expand Up @@ -1093,10 +1105,25 @@ func (c *Config) ClientSession() (interface{}, error) {
}
session.cisDomainSettingsClient, session.cisDomainSettingsErr =
cisdomainsettingsv1.NewZonesSettingsV1(cisDomainSettingsOpt)
if session.cisSSLErr != nil {
session.cisSSLErr =
fmt.Errorf("Error occured while configuring CIS SSL certificate service: %s",
session.cisSSLErr)
if session.cisDomainSettingsErr != nil {
session.cisDomainSettingsErr =
fmt.Errorf("Error occured while configuring CIS Domain Settings service: %s",
session.cisDomainSettingsErr)
}

// IBM Network CIS Routing
cisRoutingOpt := &cisroutingv1.RoutingV1Options{
URL: cisEndPoint,
Crn: core.StringPtr(""),
ZoneIdentifier: core.StringPtr(""),
Authenticator: authenticator,
}
session.cisRoutingClient, session.cisRoutingErr =
cisroutingv1.NewRoutingV1(cisRoutingOpt)
if session.cisRoutingErr != nil {
session.cisRoutingErr =
fmt.Errorf("Error occured while configuring CIS Routing service: %s",
session.cisRoutingErr)
}

return session, nil
Expand Down
2 changes: 2 additions & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func Provider() terraform.ResourceProvider {
"ibm_cis_edge_functions_action": resourceIBMCISEdgeFunctionsAction(),
"ibm_cis_edge_functions_trigger": resourceIBMCISEdgeFunctionsTrigger(),
"ibm_cis_tls_settings": resourceIBMCISTLSSettings(),
"ibm_cis_routing": resourceIBMCISRouting(),
"ibm_compute_autoscale_group": resourceIBMComputeAutoScaleGroup(),
"ibm_compute_autoscale_policy": resourceIBMComputeAutoScalePolicy(),
"ibm_compute_bare_metal": resourceIBMComputeBareMetal(),
Expand Down Expand Up @@ -481,6 +482,7 @@ func Validator() ValidatorDict {
"ibm_cis_rate_limit": resourceIBMCISRateLimitValidator(),
"ibm_cis_domain_settings": resourceIBMCISDomainSettingValidator(),
"ibm_cis_tls_settings": resourceIBMCISTLSSettingsValidator(),
"ibm_cis_routing": resourceIBMCISRoutingValidator(),
"ibm_tg_gateway": resourceIBMTGValidator(),
"ibm_tg_connection": resourceIBMTransitGatewayConnectionValidator(),
"ibm_dl_virtual_connection": resourceIBMdlGatewayVCValidator(),
Expand Down
2 changes: 0 additions & 2 deletions ibm/resource_ibm_cis_global_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ func resourceIBMCISGlb() *schema.Resource {
Type: schema.TypeString,
},
Description: "List of default Pool IDs",
//ValidateFunc: validation.StringLenBetween(1, 32),
},
cisGLBDesc: {
Type: schema.TypeString,
Optional: true,
Description: "Description for the load balancer instance",
//ValidateFunc: validation.StringLenBetween(0, 1024),
},
cisGLBTTL: {
Type: schema.TypeInt,
Expand Down
6 changes: 2 additions & 4 deletions ibm/resource_ibm_cis_global_load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func TestAccIBMCisGlb_Basic(t *testing.T) {
CheckDestroy: testAccCheckCisGlbDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCisGlbConfigCisDSBasic("test", cisDomainStatic),
ExpectNonEmptyPlan: true,
Config: testAccCheckCisGlbConfigCisDSBasic("test", cisDomainStatic),
Check: resource.ComposeTestCheckFunc(
testAccCheckCisGlbExists(name, &glb),
// dont check that specified values are set, this will be evident by lack of plan diff
Expand All @@ -35,8 +34,7 @@ func TestAccIBMCisGlb_Basic(t *testing.T) {
),
},
{
Config: testAccCheckCisGlbConfigCisDSUpdate("test", cisDomainStatic),
ExpectNonEmptyPlan: true,
Config: testAccCheckCisGlbConfigCisDSUpdate("test", cisDomainStatic),
Check: resource.ComposeTestCheckFunc(
testAccCheckCisGlbExists(name, &glb),
// dont check that specified values are set, this will be evident by lack of plan diff
Expand Down
111 changes: 111 additions & 0 deletions ibm/resource_ibm_cis_routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package ibm

import (
"log"

"github.com/IBM/go-sdk-core/v3/core"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const (
ibmCISRouting = "ibm_cis_routing"
cisRoutingSmartRouting = "smart_routing"
)

func resourceIBMCISRouting() *schema.Resource {
return &schema.Resource{
Create: resourceIBMCISRoutingUpdate,
Read: resourceIBMCISRoutingRead,
Update: resourceIBMCISRoutingUpdate,
Delete: resourceIBMCISRoutingDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Required: true,
Description: "CIS Intance CRN",
},
cisDomainID: {
Type: schema.TypeString,
Required: true,
Description: "CIS Domain ID",
DiffSuppressFunc: suppressDomainIDDiff,
},
cisRoutingSmartRouting: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Smart Routing value",
ValidateFunc: InvokeValidator(ibmCISRouting, cisRoutingSmartRouting),
},
},
}
}

func resourceIBMCISRoutingValidator() *ResourceValidator {

validateSchema := make([]ValidateSchema, 1)
smartRoutingValues := "on, off"

validateSchema = append(validateSchema,
ValidateSchema{
Identifier: cisRoutingSmartRouting,
ValidateFunctionIdentifier: ValidateAllowedStringValue,
Type: TypeString,
Required: true,
AllowedValues: smartRoutingValues})
ibmCISRoutingValidator := ResourceValidator{ResourceName: ibmCISRouting, Schema: validateSchema}
return &ibmCISRoutingValidator
}

func resourceIBMCISRoutingUpdate(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisRoutingClientSession()
if err != nil {
return err
}

crn := d.Get(cisID).(string)
zoneID, _, err := convertTftoCisTwoVar(d.Get(cisDomainID).(string))
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneIdentifier = core.StringPtr(zoneID)

if d.HasChange(cisRoutingSmartRouting) {
smartRoutingValue := d.Get(cisRoutingSmartRouting).(string)
opt := cisClient.NewUpdateSmartRoutingOptions()
opt.SetValue(smartRoutingValue)
_, response, err := cisClient.UpdateSmartRouting(opt)
if err != nil {
log.Printf("Update smart route setting failed: %v", response)
return err
}
}

d.SetId(convertCisToTfTwoVar(zoneID, crn))
return resourceIBMCISRoutingRead(d, meta)
}

func resourceIBMCISRoutingRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisRoutingClientSession()
if err != nil {
return err
}
zoneID, crn, err := convertTftoCisTwoVar(d.Id())
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneIdentifier = core.StringPtr(zoneID)
opt := cisClient.NewGetSmartRoutingOptions()
result, response, err := cisClient.GetSmartRouting(opt)
if err != nil {
log.Printf("Get smart route setting failed: %v", response)
return err
}
d.Set(cisID, crn)
d.Set(cisDomainID, zoneID)
d.Set(cisRoutingSmartRouting, *result.Result.Value)
return nil
}

func resourceIBMCISRoutingDelete(d *schema.ResourceData, meta interface{}) error {
// Nothing to delete on CIS resource
d.SetId("")
return nil
}
72 changes: 72 additions & 0 deletions ibm/resource_ibm_cis_routing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ibm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccIBMCisRouting_Basic(t *testing.T) {
name := "ibm_cis_routing." + "test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCis(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCisRoutingConfigBasic1("test", cisDomainStatic),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "smart_routing", "on"),
),
},
{
Config: testAccCheckCisRoutingConfigBasic2("test", cisDomainStatic),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "smart_routing", "off"),
),
},
},
})
}

func TestAccIBMCisRouting_Import(t *testing.T) {
name := "ibm_cis_routing." + "test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCisRoutingConfigBasic2("test", cisDomainStatic),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "smart_routing", "off"),
),
},
{
ResourceName: name,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCisRoutingConfigBasic1(id string, cisDomainStatic string) string {
return testAccCheckIBMCisDomainDataSourceConfigBasic1() + fmt.Sprintf(`
resource "ibm_cis_routing" "%[1]s" {
cis_id = data.ibm_cis.cis.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
smart_routing = "on"
}
`, id)
}
func testAccCheckCisRoutingConfigBasic2(id string, cisDomainStatic string) string {
return testAccCheckIBMCisDomainDataSourceConfigBasic1() + fmt.Sprintf(`
resource "ibm_cis_routing" "%[1]s" {
cis_id = data.ibm_cis.cis.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
smart_routing = "off"
}
`, id)
}
9 changes: 5 additions & 4 deletions ibm/resource_ibm_cis_tls_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func resourceIBMCISTLSSettings() *schema.Resource {
Required: true,
},
cisDomainID: {
Type: schema.TypeString,
Description: "Associated CIS domain",
Required: true,
Type: schema.TypeString,
Description: "Associated CIS domain",
Required: true,
DiffSuppressFunc: suppressDomainIDDiff,
},
cisTLSSettingsUniversalSSL: {
Type: schema.TypeBool,
Expand Down Expand Up @@ -87,7 +88,7 @@ func resourceCISTLSSettingsUpdate(d *schema.ResourceData, meta interface{}) erro
return err
}
crn := d.Get(cisID).(string)
zoneID := d.Get(cisDomainID).(string)
zoneID, _, err := convertTftoCisTwoVar(d.Get(cisDomainID).(string))
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneIdentifier = core.StringPtr(zoneID)

Expand Down
Loading

0 comments on commit 2df6f64

Please sign in to comment.