Skip to content

Commit

Permalink
Handle invalid context errors
Browse files Browse the repository at this point in the history
When a resource or datasource is defined with an invalid context, e.g
with a VPC for a resource with no VPC implementation, the provider
crashes as the client creation is not validated.

This PR addresses this issue.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jun 11, 2024
1 parent 2077922 commit cd7b596
Show file tree
Hide file tree
Showing 65 changed files with 692 additions and 4 deletions.
3 changes: 3 additions & 0 deletions nsxt/data_source_nsxt_policy_gateway_interface_realization.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func dataSourceNsxtPolicyGatewayInterfaceRealization() *schema.Resource {
func dataSourceNsxtPolicyGatewayInterfaceRealizationRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := realizedstate.NewRealizedEntitiesClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}

id := d.Get("id").(string)
gatewayPath := d.Get("gateway_path").(string)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_gateway_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func dataSourceNsxtPolicyGatewayPolicy() *schema.Resource {
// Local Manager Only
func listGatewayPolicies(context utl.SessionContext, domain string, connector client.Connector) ([]model.GatewayPolicy, error) {
client := domains.NewGatewayPoliciesClient(context, connector)
if client == nil {
return nil, policyResourceNotSupportedError()
}

var results []model.GatewayPolicy
boolFalse := false
Expand Down Expand Up @@ -104,6 +107,9 @@ func dataSourceNsxtPolicyGatewayPolicyRead(d *schema.ResourceData, m interface{}
if objID != "" {
// Get by id
client := domains.NewGatewayPoliciesClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
objGet, err := client.Get(domain, objID)
if isNotFoundError(err) {
return fmt.Errorf("Gateway Policy with ID %s was not found", objID)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_gateway_qos_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func testAccDataSourceNsxtPolicyGatewayQosProfileCreate(name string) error {
err = client.Patch(id, gmObj.(gm_model.GatewayQosProfile), nil)
} else {
client := infra.NewGatewayQosProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)
}

Expand Down Expand Up @@ -111,6 +114,9 @@ func testAccDataSourceNsxtPolicyGatewayQosProfileDeleteByName(name string) error
}
} else {
client := infra.NewGatewayQosProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func testAccDataSourceNsxtPolicyGroupCreate(domain string, name string) error {
id := newUUID()

client := domains.NewGroupsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(domain, id, obj)

if err != nil {
Expand All @@ -124,6 +127,9 @@ func testAccDataSourceNsxtPolicyGroupDeleteByName(domain string, name string) er

// Find the object by name and delete it
client := domains.NewGroupsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
objList, err := client.List(domain, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("Group", err)
Expand Down
3 changes: 3 additions & 0 deletions nsxt/data_source_nsxt_policy_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func dataSourceNsxtPolicyIPBlock() *schema.Resource {
func dataSourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := infra.NewIpBlocksClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_ip_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func testAccDataSourceNsxtPolicyIPBlockCreate(name, id, cidr string, isPrivate b
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpBlocksClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

displayName := name
description := name
Expand All @@ -89,6 +92,9 @@ func testAccDataSourceNsxtPolicyIPBlockDeleteByName(name string) error {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpBlocksClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_ip_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func testAccDataSourceNsxtPolicyIPPoolCreate(name string) error {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpPoolsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

displayName := name
description := name
Expand All @@ -87,6 +90,9 @@ func testAccDataSourceNsxtPolicyIPPoolDeleteByName(name string) error {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpPoolsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_ipv6_dad_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name string) error {
// Generate a random ID for the resource
id := newUUID()
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)

if err != nil {
Expand All @@ -86,6 +89,9 @@ func testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name string) error {
}
// Find the object by name and delete it
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_ipv6_ndra_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func testAccDataSourceNsxtPolicyIpv6NdraProfileCreate(name string) error {
id := newUUID()

client := infra.NewIpv6NdraProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)
if err != nil {
return handleCreateError("Ipv6NdraProfile", id, err)
Expand All @@ -90,6 +93,9 @@ func testAccDataSourceNsxtPolicyIpv6NdraProfileDeleteByName(name string) error {
}
// Find the object by name and delete it
client := infra.NewIpv6NdraProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_qos_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func testAccDataSourceNsxtPolicyQosProfileCreate(name string) error {
id := newUUID()

client := infra.NewQosProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)

if err != nil {
Expand All @@ -88,6 +91,9 @@ func testAccDataSourceNsxtPolicyQosProfileDeleteByName(name string) error {

// Find the object by name and delete it
client := infra.NewQosProfilesClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
objList, err := client.List(nil, nil, nil, nil, nil)
if err != nil {
return handleListError("QosProfile", err)
Expand Down
3 changes: 3 additions & 0 deletions nsxt/data_source_nsxt_policy_realization_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func dataSourceNsxtPolicyRealizationInfoRead(d *schema.ResourceData, m interface
var realizationError error
var realizationResult model.GenericPolicyRealizedResourceListResult
client := realizedstate.NewRealizedEntitiesClient(getSessionContext(d, m), connector)
if client == nil {
return nil, "ERROR", policyResourceNotSupportedError()
}
realizationResult, realizationError = client.List(path, nil)
state := "UNKNOWN"
if realizationError == nil {
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func dataSourceNsxtPolicySecurityPolicy() *schema.Resource {
// Local Manager Only
func listSecurityPolicies(context utl.SessionContext, domain string, connector client.Connector) ([]model.SecurityPolicy, error) {
client := domains.NewSecurityPoliciesClient(context, connector)
if client == nil {
return nil, policyResourceNotSupportedError()
}

var results []model.SecurityPolicy
boolFalse := false
Expand Down Expand Up @@ -86,6 +89,9 @@ func dataSourceNsxtPolicySecurityPolicyRead(d *schema.ResourceData, m interface{
if objID != "" {
// Get by id
client := domains.NewSecurityPoliciesClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
objGet, err := client.Get(domain, objID)
if isNotFoundError(err) {
return fmt.Errorf("Security Policy with ID %s was not found", objID)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_segment_realization.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf
segmentID := getPolicyIDFromPath(path)
enforcementPointPath := getPolicyEnforcementPointPath(m)
client := segments.NewStateClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
pendingStates := []string{model.SegmentConfigurationState_STATE_PENDING,
model.SegmentConfigurationState_STATE_IN_PROGRESS,
model.SegmentConfigurationState_STATE_IN_SYNC,
Expand Down Expand Up @@ -104,6 +107,9 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf
// return it in details yet. For now, we'll use segment display name, since its always
// translates to network name
segClient := infra.NewSegmentsClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
obj, err := segClient.Get(segmentID)
if err != nil {
return handleReadError(d, "Segment", segmentID, err)
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func testAccDataSourceNsxtPolicySegmentCreate(name string) error {
id := uuid.String()

client := infra.NewSegmentsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj)
if err != nil {
return fmt.Errorf("Error during Segment creation: %v", err)
Expand All @@ -93,6 +96,9 @@ func testAccDataSourceNsxtPolicySegmentDeleteByName(name string) error {
return nil
}
client := infra.NewSegmentsClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Delete(objID)

if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions nsxt/data_source_nsxt_policy_tier1_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func testAccDataSourceNsxtPolicyTier1GatewayCreate(routerName string) error {
// Generate a random ID for the resource
id := newUUID()
client := infra.NewTier1sClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj)

if err != nil {
Expand All @@ -87,6 +90,9 @@ func testAccDataSourceNsxtPolicyTier1GatewayDeleteByName(routerName string) erro

// Find the object by name
client := infra.NewTier1sClient(testAccGetSessionContext(), connector)
if client == nil {
return policyResourceNotSupportedError()
}

// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
Expand Down
3 changes: 3 additions & 0 deletions nsxt/gateway_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ func policyInfraPatch(context utl.SessionContext, obj model.Infra, connector cli
}

infraClient := nsx_policy.NewInfraClient(context, connector)
if infraClient == nil {
return policyResourceNotSupportedError()
}
return infraClient.Patch(obj, &enforceRevision)
}

Expand Down
23 changes: 23 additions & 0 deletions nsxt/resource_nsxt_policy_context_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func getPolicyAttributeSubAttributeValueSchema(subAttributeKey string) *schema.S

func resourceNsxtPolicyContextProfileExists(sessionContext utl.SessionContext, id string, connector client.Connector) (bool, error) {
client := infra.NewContextProfilesClient(sessionContext, connector)
if client == nil {
return false, policyResourceNotSupportedError()
}
_, err := client.Get(id)

if err == nil {
Expand Down Expand Up @@ -227,6 +230,9 @@ func resourceNsxtPolicyContextProfileCreate(d *schema.ResourceData, m interface{
// Create the resource using PATCH
log.Printf("[INFO] Creating ContextProfile with ID %s", id)
client := infra.NewContextProfilesClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)
if err != nil {
return handleCreateError("ContextProfile", id, err)
Expand All @@ -246,6 +252,9 @@ func resourceNsxtPolicyContextProfileRead(d *schema.ResourceData, m interface{})
}

client := infra.NewContextProfilesClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}
obj, err := client.Get(id)
if err != nil {
return handleReadError(d, "ContextProfile", id, err)
Expand Down Expand Up @@ -298,6 +307,9 @@ func resourceNsxtPolicyContextProfileUpdate(d *schema.ResourceData, m interface{

// Update the resource using PATCH
client := infra.NewContextProfilesClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(id, obj, nil)

if err != nil {
Expand All @@ -317,6 +329,9 @@ func resourceNsxtPolicyContextProfileDelete(d *schema.ResourceData, m interface{
var err error
force := true
client := infra.NewContextProfilesClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Delete(id, &force, nil)
if err != nil {
return handleDeleteError("ContextProfile", id, err)
Expand Down Expand Up @@ -388,15 +403,23 @@ func listAttributesWithKey(context utl.SessionContext, attributeKey string, m in
func listSystemAttributesWithKey(context utl.SessionContext, attributeKey *string, m interface{}) (model.PolicyContextProfileListResult, error) {
includeMarkForDeleteObjectsParam := false
connector := getPolicyConnector(m)
var policyContextProfileListResult model.PolicyContextProfileListResult
client := cont_prof.NewAttributesClient(context, connector)
if client == nil {
return policyContextProfileListResult, policyResourceNotSupportedError()
}
policyContextProfileListResult, err := client.List(attributeKey, nil, nil, &includeMarkForDeleteObjectsParam, nil, nil, nil, nil)
return policyContextProfileListResult, err
}

func listCustomAttributesWithKey(context utl.SessionContext, attributeKey *string, m interface{}) (model.PolicyContextProfileListResult, error) {
includeMarkForDeleteObjectsParam := false
connector := getPolicyConnector(m)
var policyContextProfileListResult model.PolicyContextProfileListResult
client := custom_attr.NewDefaultClient(context, connector)
if client == nil {
return policyContextProfileListResult, policyResourceNotSupportedError()
}
policyContextProfileListResult, err := client.List(attributeKey, nil, nil, &includeMarkForDeleteObjectsParam, nil, nil, nil, nil)
return policyContextProfileListResult, err
}
Expand Down
9 changes: 9 additions & 0 deletions nsxt/resource_nsxt_policy_context_profile_custom_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func resourceNsxtPolicyContextProfileCustomAttributeExists(sessionContext utl.Se
key, attribute := splitCustomAttributeID(id)
source := model.PolicyCustomAttributes_ATTRIBUTE_SOURCE_CUSTOM
client := infra.NewDefaultClient(sessionContext, connector)
if client == nil {
return false, policyResourceNotSupportedError()
}
attrList, err = client.List(&key, &source, nil, nil, nil, nil, nil, nil)
if err != nil {
return false, err
Expand Down Expand Up @@ -133,6 +136,9 @@ func resourceNsxtPolicyContextProfileCustomAttributeCreate(d *schema.ResourceDat

// PATCH the resource
client := infra.NewDefaultClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Create(obj, "add")
if err != nil {
return handleCreateError("ContextProfileCustomAttribute", attribute, err)
Expand Down Expand Up @@ -166,6 +172,9 @@ func resourceNsxtPolicyContextProfileCustomAttributeDelete(d *schema.ResourceDat

// PATCH the resource
client := infra.NewDefaultClient(getSessionContext(d, m), connector)
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Create(obj, "remove")

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions nsxt/resource_nsxt_policy_context_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ func nsxtPolicyContextProfileExists(resourceID string) error {
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
var err error
nsxClient := infra.NewContextProfilesClient(testAccGetSessionContext(), connector)
if nsxClient == nil {
return policyResourceNotSupportedError()
}
_, err = nsxClient.Get(resourceID)

return err
Expand Down
Loading

0 comments on commit cd7b596

Please sign in to comment.