-
Notifications
You must be signed in to change notification settings - Fork 666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ibm_event_streams_schema_global_rule #5621
Open
kccox
wants to merge
4
commits into
IBM-Cloud:master
Choose a base branch
from
kccox:add-global-compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
118 changes: 118 additions & 0 deletions
118
ibm/service/eventstreams/data_source_ibm_event_streams_schema_global_compatibility_rule.go
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,118 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package eventstreams | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" | ||
"github.com/IBM/eventstreams-go-sdk/pkg/schemaregistryv1" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
schemaGlobalCompatibilityRuleResourceType = "schema-global-compatibility-rule" | ||
schemaCompatibilityRuleType = "COMPATIBILITY" | ||
schemaCompatibilityDefaultValue = "NONE" | ||
) | ||
|
||
var ( | ||
schemaCompatiblityRuleValidConfigValues = []string{schemaCompatibilityDefaultValue, "FULL", "FULL_TRANSITIVE", "FORWARD", "FORWARD_TRANSITIVE", "BACKWARD", "BACKWARD_TRANSITIVE"} | ||
) | ||
|
||
// The global compatibility rule in an Event Streams service instance. | ||
// The ID is the CRN with the last two components "schema-global-compatibility:". | ||
func DataSourceIBMEventStreamsSchemaGlobalCompatibilityRule() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_instance_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The ID or CRN of the Event Streams service instance", | ||
}, | ||
"config": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The value of the global schema compatibility rule", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// read global compatibility rule properties using the schema registry API | ||
func dataSourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession() | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry session", "ibm_event_streams_schema_global_rule", "read") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
adminURL, instanceCRN, err := getSchemaRuleInstanceURL(d, meta) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry URL", "ibm_event_streams_schema_global_rule", "read") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
schemaregistryClient.SetServiceURL(adminURL) | ||
|
||
getOpts := &schemaregistryv1.GetGlobalRuleOptions{} | ||
getOpts.SetRule(schemaCompatibilityRuleType) | ||
rule, _, err := schemaregistryClient.GetGlobalRuleWithContext(context, getOpts) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "GetGlobalRule returned error", "ibm_event_streams_schema_global_rule", "read") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
if rule.Config == nil { | ||
tfErr := flex.TerraformErrorf(err, "Unexpected nil config when getting global compatibility rule", "ibm_event_streams_schema_global_rule", "read") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
d.SetId(getSchemaGlobalCompatibilityRuleID(instanceCRN)) | ||
d.Set("resource_instance_id", instanceCRN) | ||
d.Set("config", *rule.Config) | ||
return nil | ||
} | ||
|
||
func getSchemaRuleInstanceURL(d *schema.ResourceData, meta interface{}) (string, string, error) { | ||
instanceCRN := d.Get("resource_instance_id").(string) | ||
if instanceCRN == "" { // importing | ||
id := d.Id() | ||
crnSegments := strings.Split(id, ":") | ||
if len(crnSegments) != 10 || crnSegments[8] != schemaGlobalCompatibilityRuleResourceType { | ||
return "", "", fmt.Errorf("ID '%s' is not a schema global compatibility resource", id) | ||
} | ||
crnSegments[8] = "" | ||
crnSegments[9] = "" | ||
instanceCRN = strings.Join(crnSegments, ":") | ||
d.Set("resource_instance_id", instanceCRN) | ||
} | ||
|
||
instance, err := getInstanceDetails(instanceCRN, meta) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
adminURL := instance.Extensions["kafka_http_url"].(string) | ||
planID := *instance.ResourcePlanID | ||
valid := strings.Contains(planID, "enterprise") | ||
if !valid { | ||
return "", "", fmt.Errorf("schema registry is not supported by the Event Streams %s plan, enterprise plan is expected", | ||
planID) | ||
} | ||
return adminURL, instanceCRN, nil | ||
} | ||
|
||
func getSchemaGlobalCompatibilityRuleID(instanceCRN string) string { | ||
crnSegments := strings.Split(instanceCRN, ":") | ||
crnSegments[8] = schemaGlobalCompatibilityRuleResourceType | ||
crnSegments[9] = "" | ||
return strings.Join(crnSegments, ":") | ||
} |
67 changes: 67 additions & 0 deletions
67
...rvice/eventstreams/data_source_ibm_event_streams_schema_global_compatibility_rule_test.go
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,67 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package eventstreams_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccIBMEventStreamsSchemaGlobalCompatibilityRuleDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: acc.TestAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleDataSourceConfig(getTestInstanceName(mzrKey)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleProperties("data.ibm_event_streams_schema_global_rule.es_globalrule", ""), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleDataSourceConfig(instanceName string) string { | ||
return fmt.Sprintf(` | ||
data "ibm_resource_group" "group" { | ||
is_default=true | ||
} | ||
data "ibm_resource_instance" "es_instance" { | ||
resource_group_id = data.ibm_resource_group.group.id | ||
name = "%s" | ||
} | ||
data "ibm_event_streams_schema_global_rule" "es_globalrule" { | ||
resource_instance_id = data.ibm_resource_instance.es_instance.id | ||
}`, instanceName) | ||
} | ||
|
||
// check properties of the global compatibility rule data source or resource object | ||
func testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleProperties(name string, expectConfig string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
gcrID := rs.Primary.ID | ||
if gcrID == "" { | ||
return fmt.Errorf("[ERROR] Global compatibility rule ID is not set") | ||
} | ||
if !strings.HasSuffix(gcrID, ":schema-global-compatibility-rule:") { | ||
return fmt.Errorf("[ERROR] Global compatibility rule ID %s not expected CRN", gcrID) | ||
} | ||
config := rs.Primary.Attributes["config"] | ||
if config == "" { | ||
return fmt.Errorf("[ERROR] Global compatibility config is not defined") | ||
} | ||
if expectConfig != "" && config != expectConfig { | ||
return fmt.Errorf("[ERROR] Global compatibility config is %s, expected %s", config, expectConfig) | ||
} | ||
return nil | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
ibm/service/eventstreams/resource_ibm_event_streams_schema_global_compatibility_rule.go
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 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package eventstreams | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" | ||
"github.com/IBM/eventstreams-go-sdk/pkg/schemaregistryv1" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
// The global compatibility rule in an Event Streams service instance. | ||
// The ID is the CRN with the last two components "schema-global-compatibility:". | ||
// The rule is the schema compatibility rule, one of the validRules values. | ||
func ResourceIBMEventStreamsSchemaGlobalCompatibilityRule() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceIBMEventStreamsSchemaGlobalCompatibilityRuleUpdate, | ||
ReadContext: resourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead, | ||
UpdateContext: resourceIBMEventStreamsSchemaGlobalCompatibilityRuleUpdate, | ||
DeleteContext: resourceIBMEventStreamsSchemaGlobalCompatibilityRuleDelete, | ||
Importer: &schema.ResourceImporter{}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_instance_id": { | ||
Type: schema.TypeString, | ||
Description: "The ID or the CRN of the Event Streams service instance", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"config": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(schemaCompatiblityRuleValidConfigValues, true), | ||
Description: "The value of the global schema compatibility rule", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return dataSourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead(context, d, meta) | ||
} | ||
|
||
// The global compatibility rule is always defined in the schema registry, | ||
// so create and update have the same behavior | ||
func resourceIBMEventStreamsSchemaGlobalCompatibilityRuleUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession() | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry session", "ibm_event_streams_schema_global_rule", "update") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
adminURL, _, err := getSchemaRuleInstanceURL(d, meta) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry URL", "ibm_event_streams_schema_global_rule", "update") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
schemaregistryClient.SetServiceURL(adminURL) | ||
|
||
updateOpts := &schemaregistryv1.UpdateGlobalRuleOptions{} | ||
updateOpts.SetType(schemaCompatibilityRuleType) | ||
updateOpts.SetRule(schemaCompatibilityRuleType) | ||
updateOpts.SetConfig(d.Get("config").(string)) | ||
|
||
_, _, err = schemaregistryClient.UpdateGlobalRuleWithContext(context, updateOpts) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "UpdateGlobalRule returned error", "ibm_event_streams_schema_global_rule", "update") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
return resourceIBMEventStreamsSchemaGlobalCompatibilityRuleRead(context, d, meta) | ||
} | ||
|
||
// The global rule can't be deleted, but we reset it to the default NONE. | ||
func resourceIBMEventStreamsSchemaGlobalCompatibilityRuleDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession() | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry session", "ibm_event_streams_schema_global_rule", "delete") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
adminURL, _, err := getSchemaRuleInstanceURL(d, meta) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "Error getting Event Streams schema registry URL", "ibm_event_streams_schema_global_rule", "delete") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
schemaregistryClient.SetServiceURL(adminURL) | ||
|
||
updateOpts := &schemaregistryv1.UpdateGlobalRuleOptions{} | ||
updateOpts.SetType(schemaCompatibilityRuleType) | ||
updateOpts.SetRule(schemaCompatibilityRuleType) | ||
updateOpts.SetConfig("NONE") | ||
|
||
_, _, err = schemaregistryClient.UpdateGlobalRuleWithContext(context, updateOpts) | ||
if err != nil { | ||
tfErr := flex.TerraformErrorf(err, "UpdateGlobalRule returned error", "ibm_event_streams_schema_global_rule", "delete") | ||
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) | ||
return tfErr.GetDiag() | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
70 changes: 70 additions & 0 deletions
70
ibm/service/eventstreams/resource_ibm_event_streams_schema_global_compatibility_rule_test.go
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,70 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package eventstreams_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" | ||
"github.com/IBM/eventstreams-go-sdk/pkg/schemaregistryv1" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccIBMEventStreamsSchemaGlobalCompatibilityRuleResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheck(t) }, | ||
Providers: acc.TestAccProviders, | ||
CheckDestroy: testAccCheckIBMEventStreamsGlobalCompatibilityRuleResetToNoneInInstance(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleResourceConfig(getTestInstanceName(mzrKey), "FORWARD"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleProperties("ibm_event_streams_schema_global_rule.es_globalrule", "FORWARD"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "ibm_event_streams_schema_global_rule.es_globalrule", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMEventStreamsSchemaGlobalCompatibilityRuleResourceConfig(instanceName string, rule string) string { | ||
return fmt.Sprintf(` | ||
data "ibm_resource_group" "group" { | ||
is_default=true | ||
} | ||
data "ibm_resource_instance" "es_instance" { | ||
resource_group_id = data.ibm_resource_group.group.id | ||
name = "%s" | ||
} | ||
resource "ibm_event_streams_schema_global_rule" "es_globalrule" { | ||
resource_instance_id = data.ibm_resource_instance.es_instance.id | ||
config = "%s" | ||
}`, instanceName, rule) | ||
} | ||
|
||
func testAccCheckIBMEventStreamsGlobalCompatibilityRuleResetToNoneInInstance() resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
schemaregistryClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).ESschemaRegistrySession() | ||
if err != nil { | ||
return err | ||
} | ||
getOpts := &schemaregistryv1.GetGlobalRuleOptions{} | ||
getOpts.SetRule("COMPATIBILITY") | ||
rule, _, err := schemaregistryClient.GetGlobalRule(getOpts) | ||
if err != nil { | ||
return err | ||
} | ||
if rule.Config == nil || *rule.Config != "NONE" { | ||
return fmt.Errorf("[ERROR] Expected global compatibility rule reset to NONE after deletion") | ||
} | ||
return nil | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we are appending empty string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our convention in Event Streams is that the first 8 fields of the CRN identify the service-instance, and the last two identify the resource on the instance.
crnSegment[8]
is the type of the resource, andcrnSegment[9]
is the identifier for the resource.The type for the global rule is
schema-global-compatibility-rule
. Since the global compatibility rule is global, there is only one instance, so it does not have a unique identifier field. So we set segment 9 to""
.Example:
crn:v1:staging:public:messagehub-vnext-integration:eu-gb:a/6db1b0d0b5c54ee5c201552547febcd8:4088c6f5-6a6b-49e8-a31d-712f09f23058::
crn:v1:staging:public:messagehub-vnext-integration:eu-gb:a/6db1b0d0b5c54ee5c201552547febcd8:4088c6f5-6a6b-49e8-a31d-712f09f23058:schema-global-compatibility-rule:
In practice, the split segment 9 should already be
""
since it should be coming from a service-instance CRN, in which the last two fields are not set. Setting it to""
is a precaution in case some other value was provided.