-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf33718
commit 2df6f64
Showing
16 changed files
with
664 additions
and
17 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
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
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
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,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 | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.