diff --git a/nsxt/data_source_nsxt_policy_bfd_profile.go b/nsxt/data_source_nsxt_policy_bfd_profile.go index 64e68a238..7ef1ae729 100644 --- a/nsxt/data_source_nsxt_policy_bfd_profile.go +++ b/nsxt/data_source_nsxt_policy_bfd_profile.go @@ -23,7 +23,11 @@ func dataSourceNsxtPolicyBfdProfile() *schema.Resource { func dataSourceNsxtPolicyBfdProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "BfdProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "BfdProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_bridge_profile.go b/nsxt/data_source_nsxt_policy_bridge_profile.go index b0fc49b71..1252d4258 100644 --- a/nsxt/data_source_nsxt_policy_bridge_profile.go +++ b/nsxt/data_source_nsxt_policy_bridge_profile.go @@ -23,7 +23,11 @@ func dataSourceNsxtPolicyBridgeProfile() *schema.Resource { func dataSourceNsxtPolicyBridgeProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "L2BridgeEndpointProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "L2BridgeEndpointProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_certificate.go b/nsxt/data_source_nsxt_policy_certificate.go index 0c90279c2..604ae84e0 100644 --- a/nsxt/data_source_nsxt_policy_certificate.go +++ b/nsxt/data_source_nsxt_policy_certificate.go @@ -23,7 +23,11 @@ func dataSourceNsxtPolicyCertificate() *schema.Resource { func dataSourceNsxtPolicyCertificateRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "TlsCertificate", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "TlsCertificate", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_context_profile.go b/nsxt/data_source_nsxt_policy_context_profile.go index 61589d6d4..71751be4a 100644 --- a/nsxt/data_source_nsxt_policy_context_profile.go +++ b/nsxt/data_source_nsxt_policy_context_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyContextProfile() *schema.Resource { } func dataSourceNsxtPolicyContextProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "PolicyContextProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "PolicyContextProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_dhcp_server.go b/nsxt/data_source_nsxt_policy_dhcp_server.go index a2a9d8964..22258869b 100644 --- a/nsxt/data_source_nsxt_policy_dhcp_server.go +++ b/nsxt/data_source_nsxt_policy_dhcp_server.go @@ -24,7 +24,11 @@ func dataSourceNsxtPolicyDhcpServer() *schema.Resource { func dataSourceNsxtPolicyDhcpServerRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "DhcpServerConfig", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "DhcpServerConfig", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go b/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go index e3226c34d..a312697e4 100644 --- a/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go +++ b/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go @@ -24,7 +24,11 @@ func dataSourceNsxtPolicyDistributedFloodProtectionProfile() *schema.Resource { func dataSourceNsxtPolicyDistributedFloodProtectionProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "DistributedFloodProtectionProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "DistributedFloodProtectionProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_edge_cluster.go b/nsxt/data_source_nsxt_policy_edge_cluster.go index d72aff707..dedd1ea0c 100644 --- a/nsxt/data_source_nsxt_policy_edge_cluster.go +++ b/nsxt/data_source_nsxt_policy_edge_cluster.go @@ -41,7 +41,11 @@ func dataSourceNsxtPolicyEdgeClusterRead(d *schema.ResourceData, m interface{}) query := make(map[string]string) globalPolicyEnforcementPointPath := getGlobalPolicyEnforcementPointPath(m, &objSitePath) query["parent_path"] = globalPolicyEnforcementPointPath - _, err := policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), getSessionContext(d, m), "PolicyEdgeCluster", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), context, "PolicyEdgeCluster", query, false) if err != nil { return err } @@ -50,7 +54,11 @@ func dataSourceNsxtPolicyEdgeClusterRead(d *schema.ResourceData, m interface{}) // Local manager connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "PolicyEdgeCluster", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "PolicyEdgeCluster", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_edge_node.go b/nsxt/data_source_nsxt_policy_edge_node.go index 770762467..c05df7c77 100644 --- a/nsxt/data_source_nsxt_policy_edge_node.go +++ b/nsxt/data_source_nsxt_policy_edge_node.go @@ -50,7 +50,11 @@ func dataSourceNsxtPolicyEdgeNodeRead(d *schema.ResourceData, m interface{}) err if memberIndexSet { query["member_index"] = strconv.Itoa(memberIndex.(int)) } - obj, err := policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), getSessionContext(d, m), "PolicyEdgeNode", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), context, "PolicyEdgeNode", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go index 3b3d2c96b..a7dbd0c2b 100644 --- a/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go +++ b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go @@ -32,7 +32,11 @@ func dataSourceNsxtPolicyGatewayDNSForwarderRead(d *schema.ResourceData, m inter if len(gwPath) > 0 { query["parent_path"] = fmt.Sprintf("%s*", gwPath) } - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "PolicyDnsForwarder", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, "PolicyDnsForwarder", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go b/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go index 31c99a76c..84a8a1c45 100644 --- a/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go +++ b/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go @@ -24,7 +24,11 @@ func dataSourceNsxtPolicyGatewayFloodProtectionProfile() *schema.Resource { func dataSourceNsxtPolicyGatewayFloodProtectionProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "GatewayFloodProtectionProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "GatewayFloodProtectionProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_gateway_interface_realization.go b/nsxt/data_source_nsxt_policy_gateway_interface_realization.go index b4c7864fe..b30f22355 100644 --- a/nsxt/data_source_nsxt_policy_gateway_interface_realization.go +++ b/nsxt/data_source_nsxt_policy_gateway_interface_realization.go @@ -72,7 +72,11 @@ func dataSourceNsxtPolicyGatewayInterfaceRealization() *schema.Resource { func dataSourceNsxtPolicyGatewayInterfaceRealizationRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := realizedstate.NewRealizedEntitiesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := realizedstate.NewRealizedEntitiesClient(context, connector) id := d.Get("id").(string) gatewayPath := d.Get("gateway_path").(string) @@ -154,7 +158,7 @@ func dataSourceNsxtPolicyGatewayInterfaceRealizationRead(d *schema.ResourceData, MinTimeout: 1 * time.Second, Delay: time.Duration(delay) * time.Second, } - _, err := stateConf.WaitForState() + _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Failed to get gateway interface realization information for %s: %v", gatewayPath, err) } diff --git a/nsxt/data_source_nsxt_policy_gateway_locale_service.go b/nsxt/data_source_nsxt_policy_gateway_locale_service.go index bb14d4e29..55cc19edb 100644 --- a/nsxt/data_source_nsxt_policy_gateway_locale_service.go +++ b/nsxt/data_source_nsxt_policy_gateway_locale_service.go @@ -39,7 +39,11 @@ func dataSourceNsxtPolicyGatewayLocaleServiceRead(d *schema.ResourceData, m inte gwPath := d.Get("gateway_path").(string) query := make(map[string]string) query["parent_path"] = gwPath - obj, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "LocaleServices", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceReadWithValidation(d, connector, context, "LocaleServices", query, false) if err != nil { return err diff --git a/nsxt/data_source_nsxt_policy_gateway_policy.go b/nsxt/data_source_nsxt_policy_gateway_policy.go index 4b5adacd9..cb39a4c37 100644 --- a/nsxt/data_source_nsxt_policy_gateway_policy.go +++ b/nsxt/data_source_nsxt_policy_gateway_policy.go @@ -74,7 +74,10 @@ func dataSourceNsxtPolicyGatewayPolicyRead(d *schema.ResourceData, m interface{} category := d.Get("category").(string) domain := d.Get("domain").(string) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isPolicyGlobalManager(m) { query := make(map[string]string) query["parent_path"] = "*/" + domain diff --git a/nsxt/data_source_nsxt_policy_gateway_prefix_list.go b/nsxt/data_source_nsxt_policy_gateway_prefix_list.go index 9d49df9bc..ec73a0645 100644 --- a/nsxt/data_source_nsxt_policy_gateway_prefix_list.go +++ b/nsxt/data_source_nsxt_policy_gateway_prefix_list.go @@ -31,7 +31,11 @@ func dataSourceNsxtPolicyGatewayPrefixListRead(d *schema.ResourceData, m interfa if len(gwPath) > 0 { query["parent_path"] = fmt.Sprintf("%s*", gwPath) } - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "PrefixList", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, "PrefixList", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_gateway_qos_profile.go b/nsxt/data_source_nsxt_policy_gateway_qos_profile.go index d3846cebe..b3f877235 100644 --- a/nsxt/data_source_nsxt_policy_gateway_qos_profile.go +++ b/nsxt/data_source_nsxt_policy_gateway_qos_profile.go @@ -22,7 +22,8 @@ func dataSourceNsxtPolicyGatewayQosProfile() *schema.Resource { } func dataSourceNsxtPolicyGatewayQosProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "GatewayQosProfile", nil) + context, err := getSessionContext(d, m) + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "GatewayQosProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_gateway_route_map.go b/nsxt/data_source_nsxt_policy_gateway_route_map.go index 5ed8a5885..106895aa2 100644 --- a/nsxt/data_source_nsxt_policy_gateway_route_map.go +++ b/nsxt/data_source_nsxt_policy_gateway_route_map.go @@ -31,7 +31,11 @@ func dataSourceNsxtPolicyGatewayRouteMapRead(d *schema.ResourceData, m interface if len(gwPath) > 0 { query["parent_path"] = fmt.Sprintf("%s*", gwPath) } - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "Tier0RouteMap", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, "Tier0RouteMap", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_group.go b/nsxt/data_source_nsxt_policy_group.go index 1a66ed36c..bc58826b2 100644 --- a/nsxt/data_source_nsxt_policy_group.go +++ b/nsxt/data_source_nsxt_policy_group.go @@ -26,7 +26,11 @@ func dataSourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error domain := d.Get("domain").(string) query := make(map[string]string) query["parent_path"] = "*/" + domain - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Group", query) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Group", query) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_host_transport_node.go b/nsxt/data_source_nsxt_policy_host_transport_node.go index 6aad09a2b..6583366d8 100644 --- a/nsxt/data_source_nsxt_policy_host_transport_node.go +++ b/nsxt/data_source_nsxt_policy_host_transport_node.go @@ -28,7 +28,11 @@ func dataSourceNsxtPolicyHostTransportNode() *schema.Resource { } func dataSourceNsxtPolicyHostTransportNodeRead(d *schema.ResourceData, m interface{}) error { - obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "HostTransportNode", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), context, "HostTransportNode", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_host_transport_node_profile.go b/nsxt/data_source_nsxt_policy_host_transport_node_profile.go index 71ad15580..339fd69f3 100644 --- a/nsxt/data_source_nsxt_policy_host_transport_node_profile.go +++ b/nsxt/data_source_nsxt_policy_host_transport_node_profile.go @@ -21,7 +21,11 @@ func dataSourceNsxtPolicyHostTransportNodeProfile() *schema.Resource { } func dataSourceNsxtPolicyHostTransportNodeProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "PolicyHostTransportNodeProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "PolicyHostTransportNodeProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_intrusion_service_profile.go b/nsxt/data_source_nsxt_policy_intrusion_service_profile.go index 68fe51178..ae710a302 100644 --- a/nsxt/data_source_nsxt_policy_intrusion_service_profile.go +++ b/nsxt/data_source_nsxt_policy_intrusion_service_profile.go @@ -28,7 +28,11 @@ func dataSourceNsxtPolicyIntrusionServiceProfileRead(d *schema.ResourceData, m i return localManagerOnlyError() } - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "IdsProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "IdsProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ip_block.go b/nsxt/data_source_nsxt_policy_ip_block.go index b171cb2ce..b228b8909 100644 --- a/nsxt/data_source_nsxt_policy_ip_block.go +++ b/nsxt/data_source_nsxt_policy_ip_block.go @@ -29,7 +29,11 @@ func dataSourceNsxtPolicyIPBlock() *schema.Resource { func dataSourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpBlocksClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpBlocksClient(context, connector) objID := d.Get("id").(string) objName := d.Get("display_name").(string) diff --git a/nsxt/data_source_nsxt_policy_ip_discovery_profile.go b/nsxt/data_source_nsxt_policy_ip_discovery_profile.go index 739222002..97513f8c0 100644 --- a/nsxt/data_source_nsxt_policy_ip_discovery_profile.go +++ b/nsxt/data_source_nsxt_policy_ip_discovery_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyIPDiscoveryProfile() *schema.Resource { } func dataSourceNsxtPolicyIPDiscoveryProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "IPDiscoveryProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "IPDiscoveryProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ip_pool.go b/nsxt/data_source_nsxt_policy_ip_pool.go index 0de9a4bc8..da2b5ce5c 100644 --- a/nsxt/data_source_nsxt_policy_ip_pool.go +++ b/nsxt/data_source_nsxt_policy_ip_pool.go @@ -30,7 +30,11 @@ func dataSourceNsxtPolicyIPPool() *schema.Resource { } func dataSourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error { - obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "IpAddressPool", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), context, "IpAddressPool", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ipsec_vpn_local_endpoint.go b/nsxt/data_source_nsxt_policy_ipsec_vpn_local_endpoint.go index 014d6e75d..ad5834272 100644 --- a/nsxt/data_source_nsxt_policy_ipsec_vpn_local_endpoint.go +++ b/nsxt/data_source_nsxt_policy_ipsec_vpn_local_endpoint.go @@ -49,7 +49,11 @@ func dataSourceNsxtPolicyIPSecVpnLocalEndpointRead(d *schema.ResourceData, m int } query["parent_path"] = fmt.Sprintf("%s*", servicePath) } - objInt, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "IPSecVpnLocalEndpoint", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + objInt, err := policyDataSourceResourceReadWithValidation(d, connector, context, "IPSecVpnLocalEndpoint", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ipsec_vpn_service.go b/nsxt/data_source_nsxt_policy_ipsec_vpn_service.go index 183887e1c..1be08c1cb 100644 --- a/nsxt/data_source_nsxt_policy_ipsec_vpn_service.go +++ b/nsxt/data_source_nsxt_policy_ipsec_vpn_service.go @@ -31,7 +31,11 @@ func dataSourceNsxtPolicyIPSecVpnServiceRead(d *schema.ResourceData, m interface if len(gwPath) > 0 { query["parent_path"] = fmt.Sprintf("%s*", gwPath) } - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "IPSecVpnService", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, "IPSecVpnService", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go b/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go index c2432f9c4..9a5ea8611 100644 --- a/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go +++ b/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyIpv6DadProfile() *schema.Resource { } func dataSourceNsxtPolicyIpv6DadProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Ipv6DadProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Ipv6DadProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go b/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go index b0890b911..5570317a9 100644 --- a/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go +++ b/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyIpv6NdraProfile() *schema.Resource { } func dataSourceNsxtPolicyIpv6NdraProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Ipv6NdraProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Ipv6NdraProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_l2_vpn_service.go b/nsxt/data_source_nsxt_policy_l2_vpn_service.go index 5598d050d..aa9fb0386 100644 --- a/nsxt/data_source_nsxt_policy_l2_vpn_service.go +++ b/nsxt/data_source_nsxt_policy_l2_vpn_service.go @@ -31,7 +31,11 @@ func dataSourceNsxtPolicyL2VpnServiceRead(d *schema.ResourceData, m interface{}) if len(gwPath) > 0 { query["parent_path"] = fmt.Sprintf("%s*", gwPath) } - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "L2VPNService", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, "L2VPNService", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_lb_service.go b/nsxt/data_source_nsxt_policy_lb_service.go index 003e2a861..6bad617e4 100644 --- a/nsxt/data_source_nsxt_policy_lb_service.go +++ b/nsxt/data_source_nsxt_policy_lb_service.go @@ -23,7 +23,11 @@ func dataSourceNsxtPolicyLbService() *schema.Resource { func dataSourceNsxtPolicyLbServiceRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "LBService", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "LBService", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_mac_discovery_profile.go b/nsxt/data_source_nsxt_policy_mac_discovery_profile.go index 6aafbd9ff..c7ef6e63c 100644 --- a/nsxt/data_source_nsxt_policy_mac_discovery_profile.go +++ b/nsxt/data_source_nsxt_policy_mac_discovery_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyMacDiscoveryProfile() *schema.Resource { } func dataSourceNsxtPolicyMacDiscoveryProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "MacDiscoveryProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "MacDiscoveryProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_qos_profile.go b/nsxt/data_source_nsxt_policy_qos_profile.go index 1f236b7cd..4abff1b57 100644 --- a/nsxt/data_source_nsxt_policy_qos_profile.go +++ b/nsxt/data_source_nsxt_policy_qos_profile.go @@ -22,7 +22,8 @@ func dataSourceNsxtPolicyQosProfile() *schema.Resource { } func dataSourceNsxtPolicyQosProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "QoSProfile", nil) + context, err := getSessionContext(d, m) + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "QoSProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_realization_info.go b/nsxt/data_source_nsxt_policy_realization_info.go index 039931e07..85d0d82f8 100644 --- a/nsxt/data_source_nsxt_policy_realization_info.go +++ b/nsxt/data_source_nsxt_policy_realization_info.go @@ -79,6 +79,11 @@ func dataSourceNsxtPolicyRealizationInfoRead(d *schema.ResourceData, m interface delay := d.Get("delay").(int) timeout := d.Get("timeout").(int) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + // Site is mandatory got GM and irrelevant else if !isPolicyGlobalManager(m) && objSitePath != "" { return globalManagerOnlyError() @@ -104,7 +109,7 @@ func dataSourceNsxtPolicyRealizationInfoRead(d *schema.ResourceData, m interface var realizationError error var realizationResult model.GenericPolicyRealizedResourceListResult - client := realizedstate.NewRealizedEntitiesClient(getSessionContext(d, m), connector) + client := realizedstate.NewRealizedEntitiesClient(context, connector) realizationResult, realizationError = client.List(path, nil) state := "UNKNOWN" if realizationError == nil { @@ -146,7 +151,7 @@ func dataSourceNsxtPolicyRealizationInfoRead(d *schema.ResourceData, m interface MinTimeout: 1 * time.Second, Delay: time.Duration(delay) * time.Second, } - _, err := stateConf.WaitForState() + _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Failed to get realization information for %s: %v", path, err) } diff --git a/nsxt/data_source_nsxt_policy_security_policy.go b/nsxt/data_source_nsxt_policy_security_policy.go index 1714a79e5..0ea5bf7f7 100644 --- a/nsxt/data_source_nsxt_policy_security_policy.go +++ b/nsxt/data_source_nsxt_policy_security_policy.go @@ -82,7 +82,10 @@ func dataSourceNsxtPolicySecurityPolicyRead(d *schema.ResourceData, m interface{ objName := d.Get("display_name").(string) var obj model.SecurityPolicy - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if objID != "" { // Get by id client := domains.NewSecurityPoliciesClient(context, connector) diff --git a/nsxt/data_source_nsxt_policy_segment.go b/nsxt/data_source_nsxt_policy_segment.go index 430e47fac..6bee69e6f 100644 --- a/nsxt/data_source_nsxt_policy_segment.go +++ b/nsxt/data_source_nsxt_policy_segment.go @@ -24,7 +24,11 @@ func dataSourceNsxtPolicySegment() *schema.Resource { func dataSourceNsxtPolicySegmentRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "Segment", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, connector, context, "Segment", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_segment_realization.go b/nsxt/data_source_nsxt_policy_segment_realization.go index 0240a4027..369983a56 100644 --- a/nsxt/data_source_nsxt_policy_segment_realization.go +++ b/nsxt/data_source_nsxt_policy_segment_realization.go @@ -46,7 +46,10 @@ func dataSourceNsxtPolicySegmentRealization() *schema.Resource { func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interface{}) error { // Read the realization info by the path, and wait till it is valid connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } commonProviderConfig := getCommonProviderConfig(m) // Get the realization info of this resource @@ -91,7 +94,7 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf MinTimeout: 1 * time.Second, Delay: 1 * time.Second, } - _, err := stateConf.WaitForState() + _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Failed to get realization information for %s: %v", path, err) } diff --git a/nsxt/data_source_nsxt_policy_segment_security_profile.go b/nsxt/data_source_nsxt_policy_segment_security_profile.go index 48ba9fef4..ff58994e9 100644 --- a/nsxt/data_source_nsxt_policy_segment_security_profile.go +++ b/nsxt/data_source_nsxt_policy_segment_security_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicySegmentSecurityProfile() *schema.Resource { } func dataSourceNsxtPolicySegmentSecurityProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "SegmentSecurityProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "SegmentSecurityProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_service.go b/nsxt/data_source_nsxt_policy_service.go index b2ef330fa..33ccb9459 100644 --- a/nsxt/data_source_nsxt_policy_service.go +++ b/nsxt/data_source_nsxt_policy_service.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicyService() *schema.Resource { } func dataSourceNsxtPolicyServiceRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Service", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Service", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_site.go b/nsxt/data_source_nsxt_policy_site.go index 5ef2468bc..e73f87317 100644 --- a/nsxt/data_source_nsxt_policy_site.go +++ b/nsxt/data_source_nsxt_policy_site.go @@ -25,7 +25,11 @@ func dataSourceNsxtPolicySiteRead(d *schema.ResourceData, m interface{}) error { return globalManagerOnlyError() } - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Site", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Site", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_spoofguard_profile.go b/nsxt/data_source_nsxt_policy_spoofguard_profile.go index 2dd1d540e..4caf3f1ca 100644 --- a/nsxt/data_source_nsxt_policy_spoofguard_profile.go +++ b/nsxt/data_source_nsxt_policy_spoofguard_profile.go @@ -22,7 +22,11 @@ func dataSourceNsxtPolicySpoofGuardProfile() *schema.Resource { } func dataSourceNsxtPolicySpoofGuardProfileRead(d *schema.ResourceData, m interface{}) error { - _, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "SpoofGuardProfile", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceRead(d, getPolicyConnector(m), context, "SpoofGuardProfile", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_tier0_gateway.go b/nsxt/data_source_nsxt_policy_tier0_gateway.go index 5107abd2a..81dc76e73 100644 --- a/nsxt/data_source_nsxt_policy_tier0_gateway.go +++ b/nsxt/data_source_nsxt_policy_tier0_gateway.go @@ -36,7 +36,11 @@ func dataSourceNsxtPolicyTier0Gateway() *schema.Resource { func dataSourceNsxtPolicyTier0GatewayRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - obj, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "Tier0", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceRead(d, connector, context, "Tier0", nil) if err != nil { return err } @@ -51,7 +55,7 @@ func dataSourceNsxtPolicyTier0GatewayRead(d *schema.ResourceData, m interface{}) return errors[0] } gw := dataValue.(model.Tier0) - err := resourceNsxtPolicyTier0GatewayReadEdgeCluster(getSessionContext(d, m), d, connector) + err := resourceNsxtPolicyTier0GatewayReadEdgeCluster(context, d, connector) if err != nil { return fmt.Errorf("failed to get Tier0 %s locale-services: %v", *gw.Id, err) } diff --git a/nsxt/data_source_nsxt_policy_tier1_gateway.go b/nsxt/data_source_nsxt_policy_tier1_gateway.go index 8035d64ef..4f6ca9e3c 100644 --- a/nsxt/data_source_nsxt_policy_tier1_gateway.go +++ b/nsxt/data_source_nsxt_policy_tier1_gateway.go @@ -33,7 +33,11 @@ func dataSourceNsxtPolicyTier1Gateway() *schema.Resource { func dataSourceNsxtPolicyTier1GatewayRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - obj, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "Tier1", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceRead(d, connector, context, "Tier1", nil) if err != nil { return err } @@ -48,7 +52,7 @@ func dataSourceNsxtPolicyTier1GatewayRead(d *schema.ResourceData, m interface{}) return errors[0] } tier1 := dataValue.(model.Tier1) - err := resourceNsxtPolicyTier1GatewayReadEdgeCluster(getSessionContext(d, m), d, connector) + err := resourceNsxtPolicyTier1GatewayReadEdgeCluster(context, d, connector) if err != nil { return fmt.Errorf("failed to get Tier1 %s locale-services: %v", *tier1.Id, err) } diff --git a/nsxt/data_source_nsxt_policy_transport_zone.go b/nsxt/data_source_nsxt_policy_transport_zone.go index 50f43dcdb..80c77de65 100644 --- a/nsxt/data_source_nsxt_policy_transport_zone.go +++ b/nsxt/data_source_nsxt_policy_transport_zone.go @@ -76,7 +76,11 @@ func dataSourceNsxtPolicyTransportZoneRead(d *schema.ResourceData, m interface{} if isDefault { query["is_default"] = "true" } - obj, err := policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), getSessionContext(d, m), "PolicyTransportZone", query, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceReadWithValidation(d, getPolicyConnector(m), context, "PolicyTransportZone", query, false) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_uplink_host_switch_profile.go b/nsxt/data_source_nsxt_policy_uplink_host_switch_profile.go index dd328adec..d3bf08ffe 100644 --- a/nsxt/data_source_nsxt_policy_uplink_host_switch_profile.go +++ b/nsxt/data_source_nsxt_policy_uplink_host_switch_profile.go @@ -26,7 +26,11 @@ func dataSourceNsxtUplinkHostSwitchProfile() *schema.Resource { func dataSourceNsxtUplinkHostSwitchProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), infra.HostSwitchProfiles_LIST_HOSTSWITCH_PROFILE_TYPE_POLICYUPLINKHOSTSWITCHPROFILE, nil, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, infra.HostSwitchProfiles_LIST_HOSTSWITCH_PROFILE_TYPE_POLICYUPLINKHOSTSWITCHPROFILE, nil, false) if err == nil { return nil } diff --git a/nsxt/data_source_nsxt_policy_vm.go b/nsxt/data_source_nsxt_policy_vm.go index 4ac6aa002..63a64ec79 100644 --- a/nsxt/data_source_nsxt_policy_vm.go +++ b/nsxt/data_source_nsxt_policy_vm.go @@ -46,7 +46,10 @@ func dataSourceNsxtPolicyVMIDRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) objID := getNsxtPolicyVMIDFromSchema(d) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if objID != "" { vmObj, err := findNsxtPolicyVMByID(context, connector, objID, m) if err != nil { diff --git a/nsxt/data_source_nsxt_policy_vms.go b/nsxt/data_source_nsxt_policy_vms.go index 3ee1cd002..cd49699e6 100644 --- a/nsxt/data_source_nsxt_policy_vms.go +++ b/nsxt/data_source_nsxt_policy_vms.go @@ -71,7 +71,11 @@ func dataSourceNsxtPolicyVMsRead(d *schema.ResourceData, m interface{}) error { osPrefix := d.Get("guest_os").(string) vmMap := make(map[string]interface{}) - allVMs, err := listAllPolicyVirtualMachines(getSessionContext(d, m), connector, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + allVMs, err := listAllPolicyVirtualMachines(context, connector, m) if err != nil { return fmt.Errorf("Error reading Virtual Machines: %v", err) } diff --git a/nsxt/data_source_nsxt_policy_vpc.go b/nsxt/data_source_nsxt_policy_vpc.go index 036ed07d5..dc59ecf8a 100644 --- a/nsxt/data_source_nsxt_policy_vpc.go +++ b/nsxt/data_source_nsxt_policy_vpc.go @@ -27,7 +27,11 @@ func dataSourceNsxtPolicyVPC() *schema.Resource { } func dataSourceNsxtPolicyVPCRead(d *schema.ResourceData, m interface{}) error { - obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Vpc", nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), context, "Vpc", nil) if err != nil { return err } diff --git a/nsxt/data_source_nsxt_policy_vtep_ha_host_switch_profile.go b/nsxt/data_source_nsxt_policy_vtep_ha_host_switch_profile.go index 5d8357ffb..e3c6339bd 100644 --- a/nsxt/data_source_nsxt_policy_vtep_ha_host_switch_profile.go +++ b/nsxt/data_source_nsxt_policy_vtep_ha_host_switch_profile.go @@ -26,7 +26,11 @@ func dataSourceNsxtVtepHAHostSwitchProfile() *schema.Resource { func dataSourceNsxtVtepHAHostSwitchProfileRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), infra.HostSwitchProfiles_LIST_HOSTSWITCH_PROFILE_TYPE_POLICYVTEPHAHOSTSWITCHPROFILE, nil, false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + _, err = policyDataSourceResourceReadWithValidation(d, connector, context, infra.HostSwitchProfiles_LIST_HOSTSWITCH_PROFILE_TYPE_POLICYVTEPHAHOSTSWITCHPROFILE, nil, false) if err == nil { return nil } diff --git a/nsxt/policy_utils.go b/nsxt/policy_utils.go index ab27f291c..773a02673 100644 --- a/nsxt/policy_utils.go +++ b/nsxt/policy_utils.go @@ -34,7 +34,11 @@ func getOrGenerateID2(d *schema.ResourceData, m interface{}, presenceChecker fun return newUUID(), nil } - exists, err := presenceChecker(getSessionContext(d, m), id, connector) + context, err := getSessionContext(d, m) + if err != nil { + return "", err + } + exists, err := presenceChecker(context, id, connector) if err != nil { return "", err } diff --git a/nsxt/provider.go b/nsxt/provider.go index a379a316c..0f342cadf 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -785,7 +785,10 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients) clientAuthDefined := (len(clientAuthCertFile) > 0) || (len(clientAuthCert) > 0) policyEnforcementPoint := d.Get("enforcement_point").(string) policyGlobalManager := d.Get("global_manager").(bool) - projectID, vpcID := getContextDataFromSchema(d, *clients) + projectID, vpcID, err := getContextDataFromSchema(d, *clients) + if err != nil { + return err + } vmcInfo := getVmcAuthInfo(d) isVMC := false @@ -1215,9 +1218,12 @@ func getGlobalPolicyEnforcementPointPath(m interface{}, sitePath *string) string return fmt.Sprintf("%s/enforcement-points/%s", *sitePath, getPolicyEnforcementPoint(m)) } -func getContextDataFromSchema(d *schema.ResourceData, m interface{}) (string, string) { +func getContextDataFromSchema(d *schema.ResourceData, m interface{}) (string, string, error) { ctxPtr := d.Get("context") if ctxPtr != nil { + if m.(nsxtClients).ProjectID != "" || m.(nsxtClients).VPCID != "" { + return "", "", fmt.Errorf("cannot specify context in both provider level and object level") + } contexts := ctxPtr.([]interface{}) for _, context := range contexts { data := context.(map[string]interface{}) @@ -1226,15 +1232,18 @@ func getContextDataFromSchema(d *schema.ResourceData, m interface{}) (string, st vpcID = data["vpc_id"].(string) } - return data["project_id"].(string), vpcID + return data["project_id"].(string), vpcID, nil } } - return m.(nsxtClients).ProjectID, m.(nsxtClients).VPCID + return m.(nsxtClients).ProjectID, m.(nsxtClients).VPCID, nil } -func getSessionContext(d *schema.ResourceData, m interface{}) tf_api.SessionContext { +func getSessionContext(d *schema.ResourceData, m interface{}) (tf_api.SessionContext, error) { var clientType tf_api.ClientType - projectID, vpcID := getContextDataFromSchema(d, m) + projectID, vpcID, err := getContextDataFromSchema(d, m) + if err != nil { + return tf_api.SessionContext{}, err + } if projectID != "" { clientType = tf_api.Multitenancy if vpcID != "" { @@ -1245,5 +1254,5 @@ func getSessionContext(d *schema.ResourceData, m interface{}) tf_api.SessionCont } else { clientType = tf_api.Local } - return tf_api.SessionContext{ProjectID: projectID, VPCID: vpcID, ClientType: clientType} + return tf_api.SessionContext{ProjectID: projectID, VPCID: vpcID, ClientType: clientType}, nil } diff --git a/nsxt/resource_nsxt_policy_bgp_config.go b/nsxt/resource_nsxt_policy_bgp_config.go index 6863819de..fbe7b51de 100644 --- a/nsxt/resource_nsxt_policy_bgp_config.go +++ b/nsxt/resource_nsxt_policy_bgp_config.go @@ -142,7 +142,10 @@ func resourceNsxtPolicyBgpConfigToStruct(d *schema.ResourceData, isVRF bool) (*m func resourceNsxtPolicyBgpConfigCreate(d *schema.ResourceData, m interface{}) error { // This is not a create operation on NSX, since BGP config us auto created connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } gwPath := d.Get("gateway_path").(string) isT0, gwID := parseGatewayPolicyPath(gwPath) diff --git a/nsxt/resource_nsxt_policy_context_profile.go b/nsxt/resource_nsxt_policy_context_profile.go index 474019b1a..f1732cbe3 100644 --- a/nsxt/resource_nsxt_policy_context_profile.go +++ b/nsxt/resource_nsxt_policy_context_profile.go @@ -187,7 +187,10 @@ func resourceNsxtPolicyContextProfileExists(sessionContext utl.SessionContext, i func resourceNsxtPolicyContextProfileCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } // Initialize resource Id and verify this ID is not yet used id, err := getOrGenerateID2(d, m, resourceNsxtPolicyContextProfileExists) if err != nil { @@ -245,7 +248,11 @@ func resourceNsxtPolicyContextProfileRead(d *schema.ResourceData, m interface{}) return fmt.Errorf("Error obtaining ContextProfile ID") } - client := infra.NewContextProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewContextProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "ContextProfile", id, err) @@ -264,7 +271,10 @@ func resourceNsxtPolicyContextProfileRead(d *schema.ResourceData, m interface{}) func resourceNsxtPolicyContextProfileUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } id := d.Id() if id == "" { return fmt.Errorf("Error obtaining ContextProfile ID") @@ -274,7 +284,6 @@ func resourceNsxtPolicyContextProfileUpdate(d *schema.ResourceData, m interface{ displayName := d.Get("display_name").(string) description := d.Get("description").(string) attributesStructList := make([]model.PolicyAttributes, 0) - var err error for key := range attributeKeyMap { attributes := d.Get(key).(*schema.Set).List() err := checkAttributesValid(context, attributes, m, key) @@ -316,7 +325,11 @@ func resourceNsxtPolicyContextProfileDelete(d *schema.ResourceData, m interface{ connector := getPolicyConnector(m) var err error force := true - client := infra.NewContextProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewContextProfilesClient(context, connector) err = client.Delete(id, &force, nil) if err != nil { return handleDeleteError("ContextProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go b/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go index b59681dec..02510943f 100644 --- a/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go +++ b/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go @@ -103,7 +103,11 @@ func resourceNsxtPolicyContextProfileCustomAttributeRead(d *schema.ResourceData, log.Printf("[INFO] Reading ContextProfileCustomAttribute with ID %s", d.Id()) connector := getPolicyConnector(m) - exists, err := resourceNsxtPolicyContextProfileCustomAttributeExists(getSessionContext(d, m), id, connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + exists, err := resourceNsxtPolicyContextProfileCustomAttributeExists(context, id, connector) if err != nil { return err } @@ -132,7 +136,11 @@ func resourceNsxtPolicyContextProfileCustomAttributeCreate(d *schema.ResourceDat } // PATCH the resource - client := infra.NewDefaultClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDefaultClient(context, connector) err = client.Create(obj, "add") if err != nil { return handleCreateError("ContextProfileCustomAttribute", attribute, err) @@ -165,7 +173,11 @@ func resourceNsxtPolicyContextProfileCustomAttributeDelete(d *schema.ResourceDat } // PATCH the resource - client := infra.NewDefaultClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDefaultClient(context, connector) err = client.Create(obj, "remove") if err != nil { diff --git a/nsxt/resource_nsxt_policy_dhcp_relay.go b/nsxt/resource_nsxt_policy_dhcp_relay.go index d77a08be2..76ebfbda0 100644 --- a/nsxt/resource_nsxt_policy_dhcp_relay.go +++ b/nsxt/resource_nsxt_policy_dhcp_relay.go @@ -62,7 +62,11 @@ func resourceNsxtPolicyDhcpRelayConfigExists(sessionContext utl.SessionContext, func resourceNsxtPolicyDhcpRelayConfigCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewDhcpRelayConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpRelayConfigsClient(context, connector) if client == nil { return policyResourceNotSupportedError() @@ -101,7 +105,11 @@ func resourceNsxtPolicyDhcpRelayConfigCreate(d *schema.ResourceData, m interface func resourceNsxtPolicyDhcpRelayConfigRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewDhcpRelayConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpRelayConfigsClient(context, connector) if client == nil { return policyResourceNotSupportedError() @@ -131,7 +139,11 @@ func resourceNsxtPolicyDhcpRelayConfigRead(d *schema.ResourceData, m interface{} func resourceNsxtPolicyDhcpRelayConfigUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewDhcpRelayConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpRelayConfigsClient(context, connector) if client == nil { return policyResourceNotSupportedError() } @@ -157,7 +169,7 @@ func resourceNsxtPolicyDhcpRelayConfigUpdate(d *schema.ResourceData, m interface Revision: &revision, } - _, err := client.Update(id, obj) + _, err = client.Update(id, obj) if err != nil { return handleUpdateError("DhcpRelayConfig", id, err) } @@ -172,12 +184,16 @@ func resourceNsxtPolicyDhcpRelayConfigDelete(d *schema.ResourceData, m interface } connector := getPolicyConnector(m) - client := infra.NewDhcpRelayConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpRelayConfigsClient(context, connector) if client == nil { return policyResourceNotSupportedError() } - err := client.Delete(id) + err = client.Delete(id) if err != nil { return handleDeleteError("DhcpRelayConfig", id, err) } diff --git a/nsxt/resource_nsxt_policy_dhcp_server.go b/nsxt/resource_nsxt_policy_dhcp_server.go index 89741b515..91593f31e 100644 --- a/nsxt/resource_nsxt_policy_dhcp_server.go +++ b/nsxt/resource_nsxt_policy_dhcp_server.go @@ -124,7 +124,11 @@ func resourceNsxtPolicyDhcpServerCreate(d *schema.ResourceData, m interface{}) e // Create the resource using PATCH log.Printf("[INFO] Creating DhcpServer with ID %s", id) - client := infra.NewDhcpServerConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpServerConfigsClient(context, connector) err = client.Patch(id, resourceNsxtPolicyDhcpServerSchemaToModel(d)) if err != nil { return handleCreateError("DhcpServer", id, err) @@ -144,7 +148,11 @@ func resourceNsxtPolicyDhcpServerRead(d *schema.ResourceData, m interface{}) err return fmt.Errorf("Error obtaining DhcpServer ID") } - client := infra.NewDhcpServerConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpServerConfigsClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "DhcpServer", id, err) @@ -167,7 +175,11 @@ func resourceNsxtPolicyDhcpServerRead(d *schema.ResourceData, m interface{}) err func resourceNsxtPolicyDhcpServerUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewDhcpServerConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpServerConfigsClient(context, connector) if client == nil { return policyResourceNotSupportedError() } @@ -178,7 +190,7 @@ func resourceNsxtPolicyDhcpServerUpdate(d *schema.ResourceData, m interface{}) e } // Update the resource using PATCH - err := client.Patch(id, resourceNsxtPolicyDhcpServerSchemaToModel(d)) + err = client.Patch(id, resourceNsxtPolicyDhcpServerSchemaToModel(d)) if err != nil { return handleUpdateError("DhcpServer", id, err) } @@ -194,7 +206,11 @@ func resourceNsxtPolicyDhcpServerDelete(d *schema.ResourceData, m interface{}) e var err error connector := getPolicyConnector(m) - client := infra.NewDhcpServerConfigsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDhcpServerConfigsClient(context, connector) err = client.Delete(id) if err != nil { diff --git a/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go b/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go index 3de0a969b..2e988a0d4 100644 --- a/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go +++ b/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go @@ -132,7 +132,10 @@ func policyDhcpV4StaticBindingConvertAndPatch(d *schema.ResourceData, segmentPat } connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } converter := bindings.NewTypeConverter() @@ -225,7 +228,10 @@ func resourceNsxtPolicyDhcpV4StaticBindingRead(d *schema.ResourceData, m interfa return fmt.Errorf("This resource is not applicable to segment %s", segmentPath) } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if context.ClientType == utl.Global && gwID != "" { return fmt.Errorf("This resource is not applicable to segment on Global Manager %s", segmentPath) } @@ -305,8 +311,10 @@ func resourceNsxtPolicyDhcpStaticBindingDelete(d *schema.ResourceData, m interfa _, gwID, segmentID := parseSegmentPolicyPath(segmentPath) connector := getPolicyConnector(m) - context := getSessionContext(d, m) - var err error + context, err := getSessionContext(d, m) + if err != nil { + return err + } if gwID == "" { // infra segment client := segments.NewDhcpStaticBindingConfigsClient(context, connector) diff --git a/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go b/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go index 1982565be..3db3ccaf7 100644 --- a/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go +++ b/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go @@ -125,7 +125,10 @@ func policyDhcpV6StaticBindingConvertAndPatch(d *schema.ResourceData, segmentPat if isT0 { return fmt.Errorf("This resource is not applicable to segment %s", segmentPath) } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isPolicyGlobalManager(m) && gwID != "" { return fmt.Errorf("This resource is not applicable to segment on Global Manager %s", segmentPath) @@ -185,7 +188,10 @@ func resourceNsxtPolicyDhcpV6StaticBindingRead(d *schema.ResourceData, m interfa converter := bindings.NewTypeConverter() var err error var dhcpObj *data.StructValue - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if gwID == "" { // infra segment diff --git a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go index 7b7da185c..9add20410 100644 --- a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go +++ b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go @@ -141,7 +141,11 @@ func resourceNsxtPolicyDistributedFloodProtectionProfilePatch(d *schema.Resource profileStruct := profileValue.(*data.StructValue) log.Printf("[INFO] Patching DistributedFloodProtectionProfile with ID %s", id) - client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewFloodProtectionProfilesClient(context, connector) return client.Patch(id, profileStruct, nil) } @@ -173,7 +177,11 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileRead(d *schema.ResourceD return fmt.Errorf("Error obtaining FloodProtectionProfile ID") } - client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewFloodProtectionProfilesClient(context, connector) dpffData, err := client.Get(id) if err != nil { return handleReadError(d, "FloodProtectionProfile", id, err) @@ -224,8 +232,12 @@ func resourceNsxtPolicyFloodProtectionProfileDelete(d *schema.ResourceData, m in } connector := getPolicyConnector(m) - client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector) - err := client.Delete(id, nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewFloodProtectionProfilesClient(context, connector) + err = client.Delete(id, nil) if err != nil { return handleDeleteError("FloodProtectionProfile", id, err) } diff --git a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go index 9ea95e17c..f0c8584dc 100644 --- a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go +++ b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go @@ -55,7 +55,11 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBinding() *schema.Resour func resourceNsxtPolicyDistributedFloodProtectionProfileBindingPatch(d *schema.ResourceData, m interface{}, id string, isCreate bool) error { connector := getPolicyConnector(m) - bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(context, connector) displayName := d.Get("display_name").(string) description := d.Get("description").(string) @@ -105,7 +109,11 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBindingCreate(d *schema. } groupPath := d.Get("group_path").(string) - exist, err := resourceNsxtPolicyDistributedFloodProtectionProfileBindingExists(getSessionContext(d, m), getPolicyConnector(m), groupPath, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + exist, err := resourceNsxtPolicyDistributedFloodProtectionProfileBindingExists(context, getPolicyConnector(m), groupPath, id) if err != nil { return err } @@ -131,7 +139,11 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBindingRead(d *schema.Re } connector := getPolicyConnector(m) - bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(context, connector) groupPath := d.Get("group_path").(string) domain := getDomainFromResourcePath(groupPath) @@ -168,13 +180,17 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBindingDelete(d *schema. } connector := getPolicyConnector(m) - bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + bindingClient := groups.NewFirewallFloodProtectionProfileBindingMapsClient(context, connector) groupPath := d.Get("group_path").(string) domain := getDomainFromResourcePath(groupPath) groupID := getPolicyIDFromPath(groupPath) - err := bindingClient.Delete(domain, groupID, id) + err = bindingClient.Delete(domain, groupID, id) if err != nil { return handleDeleteError("FloodProtectionProfileBinding", id, err) } diff --git a/nsxt/resource_nsxt_policy_dns_forwarder_zone.go b/nsxt/resource_nsxt_policy_dns_forwarder_zone.go index d619a1e64..3e6b9a78c 100644 --- a/nsxt/resource_nsxt_policy_dns_forwarder_zone.go +++ b/nsxt/resource_nsxt_policy_dns_forwarder_zone.go @@ -93,7 +93,11 @@ func policyDNSForwarderZonePatch(id string, d *schema.ResourceData, m interface{ } // Create the resource using PATCH - client := infra.NewDnsForwarderZonesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDnsForwarderZonesClient(context, connector) return client.Patch(id, obj) } @@ -127,7 +131,11 @@ func resourceNsxtPolicyDNSForwarderZoneRead(d *schema.ResourceData, m interface{ return fmt.Errorf("Error obtaining Dns Forwarder Zone ID") } - client := infra.NewDnsForwarderZonesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDnsForwarderZonesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "Dns Forwarder Zone", id, err) @@ -171,8 +179,12 @@ func resourceNsxtPolicyDNSForwarderZoneDelete(d *schema.ResourceData, m interfac } connector := getPolicyConnector(m) - client := infra.NewDnsForwarderZonesClient(getSessionContext(d, m), connector) - err := client.Delete(id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewDnsForwarderZonesClient(context, connector) + err = client.Delete(id) if err != nil { return handleDeleteError("Dns Forwarder Zone", id, err) diff --git a/nsxt/resource_nsxt_policy_domain.go b/nsxt/resource_nsxt_policy_domain.go index dbf2852bf..349e8b571 100644 --- a/nsxt/resource_nsxt_policy_domain.go +++ b/nsxt/resource_nsxt_policy_domain.go @@ -200,7 +200,11 @@ func resourceNsxtPolicyDomainCreate(d *schema.ResourceData, m interface{}) error // Create the resource using PATCH log.Printf("[INFO] Creating Domain with ID %s", id) - err = policyInfraPatch(getSessionContext(d, m), infraStruct, getPolicyConnector(m), false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + err = policyInfraPatch(context, infraStruct, getPolicyConnector(m), false) if err != nil { return handleCreateError("Domain", id, err) } @@ -297,7 +301,11 @@ func resourceNsxtPolicyDomainUpdate(d *schema.ResourceData, m interface{}) error ResourceType: &infraType, } - err = policyInfraPatch(getSessionContext(d, m), infraStruct, getPolicyConnector(m), false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + err = policyInfraPatch(context, infraStruct, getPolicyConnector(m), false) if err != nil { return handleUpdateError("Domain", id, err) } diff --git a/nsxt/resource_nsxt_policy_firewall_exclude_list_member.go b/nsxt/resource_nsxt_policy_firewall_exclude_list_member.go index fc2f9ec8e..d5a6bd736 100644 --- a/nsxt/resource_nsxt_policy_firewall_exclude_list_member.go +++ b/nsxt/resource_nsxt_policy_firewall_exclude_list_member.go @@ -65,8 +65,12 @@ func resourceNsxtPolicyFirewallExcludeListMemberCreate(d *schema.ResourceData, m doUpdate := func() error { var obj model.PolicyExcludeList - client := security.NewExcludeListClient(getSessionContext(d, m), connector) - obj, err := client.Get() + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := security.NewExcludeListClient(context, connector) + obj, err = client.Get() if isNotFoundError(err) { obj = model.PolicyExcludeList{ Members: []string{member}, @@ -100,7 +104,11 @@ func resourceNsxtPolicyFirewallExcludeListMemberRead(d *schema.ResourceData, m i connector := getPolicyConnector(m) member := d.Id() - client := security.NewExcludeListClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := security.NewExcludeListClient(context, connector) obj, err := client.Get() if err != nil { return handleReadError(d, "PolicyFirewallExcludeListMember", member, err) @@ -119,8 +127,12 @@ func resourceNsxtPolicyFirewallExcludeListMemberDelete(d *schema.ResourceData, m doUpdate := func() error { var obj model.PolicyExcludeList - client := security.NewExcludeListClient(getSessionContext(d, m), connector) - obj, err := client.Get() + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := security.NewExcludeListClient(context, connector) + obj, err = client.Get() if isNotFoundError(err) { return nil } else if err != nil { diff --git a/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go b/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go index 50c642b36..b363ad274 100644 --- a/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go +++ b/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go @@ -98,7 +98,10 @@ func resourceNsxtPolicyGatewayDNSForwarderRead(d *schema.ResourceData, m interfa return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -173,7 +176,10 @@ func resourceNsxtPolicyGatewayDNSForwarderCreate(d *schema.ResourceData, m inter // Verify DNS forwarder is not yet defined for this Gateway var err error - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() @@ -212,12 +218,15 @@ func resourceNsxtPolicyGatewayDNSForwarderUpdate(d *schema.ResourceData, m inter return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } log.Printf("[INFO] Updating Gateway Dns Forwarder with ID %s", gwID) - err := patchNsxtPolicyGatewayDNSForwarder(context, connector, d, gwID, isT0) + err = patchNsxtPolicyGatewayDNSForwarder(context, connector, d, gwID, isT0) if err != nil { return handleUpdateError("Gateway Dns Forwarder", gwID, err) } @@ -235,7 +244,10 @@ func resourceNsxtPolicyGatewayDNSForwarderDelete(d *schema.ResourceData, m inter } var err error - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } diff --git a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile.go b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile.go index 84f80f907..b9f11df37 100644 --- a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile.go +++ b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile.go @@ -83,7 +83,11 @@ func resourceNsxtPolicyGatewayFloodProtectionProfilePatch(d *schema.ResourceData profileStruct := profileValue.(*data.StructValue) log.Printf("[INFO] Patching GatewayFloodProtectionProfile with ID %s", id) - client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewFloodProtectionProfilesClient(context, connector) return client.Patch(id, profileStruct, nil) } @@ -115,7 +119,8 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileRead(d *schema.ResourceData, return fmt.Errorf("Error obtaining FloodProtectionProfile ID") } - client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + client := infra.NewFloodProtectionProfilesClient(context, connector) gpffData, err := client.Get(id) if err != nil { return handleReadError(d, "GatewayFloodProtectionProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go index 7461bc6f0..9965e7ea0 100644 --- a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go +++ b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go @@ -95,12 +95,17 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d *schema.Resou if err != nil { return err } + context, err := getSessionContext(d, m) + if err != nil { + return err + } + if tier0ID != "" { if localeServiceID == "" { - bindingClient := tier0s.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := tier0s.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Patch(tier0ID, id, obj) } else { - bindingClient := t0localeservices.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := t0localeservices.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Patch(tier0ID, localeServiceID, id, obj) } if err != nil { @@ -108,10 +113,10 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d *schema.Resou } } else if tier1ID != "" { if localeServiceID == "" { - bindingClient := tier1s.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := tier1s.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Patch(tier1ID, id, obj) } else { - bindingClient := t1localeservices.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := t1localeservices.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Patch(tier1ID, localeServiceID, id, obj) } } @@ -166,7 +171,11 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingCreate(d *schema.Reso id := "default" parentPath := d.Get("parent_path").(string) - exist, err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingExists(getSessionContext(d, m), getPolicyConnector(m), parentPath, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + exist, err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingExists(context, getPolicyConnector(m), parentPath, id) if err != nil { return err } @@ -194,7 +203,11 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingRead(d *schema.Resour } parentPath := d.Get("parent_path").(string) - binding, err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingGet(getSessionContext(d, m), connector, parentPath, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + binding, err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingGet(context, connector, parentPath, id) if err != nil { return handleReadError(d, "GatewayFloodProtectionProfileBinding", id, err) } @@ -247,12 +260,16 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingDelete(d *schema.Reso return err } + context, err := getSessionContext(d, m) + if err != nil { + return err + } if tier0ID != "" { if localeServiceID == "" { - bindingClient := tier0s.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := tier0s.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Delete(tier0ID, id) } else { - bindingClient := t0localeservices.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := t0localeservices.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Delete(tier0ID, localeServiceID, id) } if err != nil { @@ -260,10 +277,10 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingDelete(d *schema.Reso } } else if tier1ID != "" { if localeServiceID == "" { - bindingClient := tier1s.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := tier1s.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Delete(tier1ID, id) } else { - bindingClient := t1localeservices.NewFloodProtectionProfileBindingsClient(getSessionContext(d, m), connector) + bindingClient := t1localeservices.NewFloodProtectionProfileBindingsClient(context, connector) err = bindingClient.Delete(tier1ID, localeServiceID, id) } } diff --git a/nsxt/resource_nsxt_policy_gateway_policy.go b/nsxt/resource_nsxt_policy_gateway_policy.go index 454f09480..e63cb1009 100644 --- a/nsxt/resource_nsxt_policy_gateway_policy.go +++ b/nsxt/resource_nsxt_policy_gateway_policy.go @@ -166,7 +166,11 @@ func policyGatewayPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, con obj.Children = policyChildren } - return gatewayPolicyInfraPatch(getSessionContext(d, m), obj, domain, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + return gatewayPolicyInfraPatch(context, obj, domain, m) } func resourceNsxtPolicyGatewayPolicyCreate(d *schema.ResourceData, m interface{}) error { @@ -197,7 +201,11 @@ func resourceNsxtPolicyGatewayPolicyRead(d *schema.ResourceData, m interface{}) return fmt.Errorf("Error obtaining Gateway Policy ID") } - obj, err := getGatewayPolicyInDomain(getSessionContext(d, m), id, d.Get("domain").(string), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := getGatewayPolicyInDomain(context, id, d.Get("domain").(string), connector) if err != nil { return handleReadError(d, "Gateway Policy", id, err) } @@ -244,8 +252,12 @@ func resourceNsxtPolicyGatewayPolicyDelete(d *schema.ResourceData, m interface{} } connector := getPolicyConnector(m) - client := domains.NewGatewayPoliciesClient(getSessionContext(d, m), connector) - err := client.Delete(d.Get("domain").(string), id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGatewayPoliciesClient(context, connector) + err = client.Delete(d.Get("domain").(string), id) if err != nil { return handleDeleteError("Gateway Policy", id, err) } diff --git a/nsxt/resource_nsxt_policy_gateway_redistribution_config.go b/nsxt/resource_nsxt_policy_gateway_redistribution_config.go index 9e79044da..5d0f83aed 100644 --- a/nsxt/resource_nsxt_policy_gateway_redistribution_config.go +++ b/nsxt/resource_nsxt_policy_gateway_redistribution_config.go @@ -86,7 +86,11 @@ func policyGatewayRedistributionConfigPatch(d *schema.ResourceData, m interface{ } doPatch := func() error { - client := tier0s.NewLocaleServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := tier0s.NewLocaleServicesClient(context, connector) return client.Patch(gwID, localeServiceID, serviceStruct) } // since redistribution config is not a separate API endpoint, but sub-clause of Tier0, @@ -105,7 +109,10 @@ func resourceNsxtPolicyGatewayRedistributionConfigCreate(d *schema.ResourceData, } localeServiceID := "" - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isPolicyGlobalManager(m) { if sitePath == "" { return attributeRequiredGlobalManagerError("site_path", "nsxt_policy_gateway_redistribution_config") @@ -133,7 +140,7 @@ func resourceNsxtPolicyGatewayRedistributionConfigCreate(d *schema.ResourceData, } id := newUUID() - err := policyGatewayRedistributionConfigPatch(d, m, gwID, localeServiceID) + err = policyGatewayRedistributionConfigPatch(d, m, gwID, localeServiceID) if err != nil { return handleCreateError("Tier0 Redistribution Config", id, err) } @@ -155,7 +162,11 @@ func resourceNsxtPolicyGatewayRedistributionConfigRead(d *schema.ResourceData, m return fmt.Errorf("Error obtaining Tier0 Gateway id or Locale Service id") } - client := tier0s.NewLocaleServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := tier0s.NewLocaleServicesClient(context, connector) obj, err := client.Get(gwID, localeServiceID) if err != nil { return handleReadError(d, "Tier0 Redistribution Config", id, err) @@ -206,7 +217,11 @@ func resourceNsxtPolicyGatewayRedistributionConfigDelete(d *schema.ResourceData, // Update the locale service with empty Redistribution config using get/post doUpdate := func() error { - client := tier0s.NewLocaleServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := tier0s.NewLocaleServicesClient(context, connector) obj, err := client.Get(gwID, localeServiceID) if err != nil { return err @@ -236,7 +251,11 @@ func resourceNsxtPolicyGatewayRedistributionConfigImport(d *schema.ResourceData, gwID := s[0] localeServiceID := s[1] connector := getPolicyConnector(m) - client := tier0s.NewLocaleServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + client := tier0s.NewLocaleServicesClient(context, connector) obj, err := client.Get(gwID, localeServiceID) if err != nil || obj.RouteRedistributionConfig == nil { return nil, fmt.Errorf("Failed to retrieve redistribution config for locale service %s on gateway %s", localeServiceID, gwID) diff --git a/nsxt/resource_nsxt_policy_group.go b/nsxt/resource_nsxt_policy_group.go index 666fac7a0..98a129118 100644 --- a/nsxt/resource_nsxt_policy_group.go +++ b/nsxt/resource_nsxt_policy_group.go @@ -879,7 +879,11 @@ func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error obj.GroupType = groupTypes } - client := domains.NewGroupsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGroupsClient(context, connector) err = client.Patch(d.Get("domain").(string), id, obj) // Create the resource using PATCH @@ -901,7 +905,11 @@ func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error { if id == "" { return fmt.Errorf("Error obtaining Group ID") } - client := domains.NewGroupsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGroupsClient(context, connector) obj, err := client.Get(domainName, id) if err != nil { return handleReadError(d, "Group", id, err) @@ -992,7 +1000,11 @@ func resourceNsxtPolicyGroupUpdate(d *schema.ResourceData, m interface{}) error obj.GroupType = groupTypes } - client := domains.NewGroupsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGroupsClient(context, connector) // Update the resource using PATCH err = client.Patch(d.Get("domain").(string), id, obj) @@ -1014,7 +1026,11 @@ func resourceNsxtPolicyGroupDelete(d *schema.ResourceData, m interface{}) error failIfSubtreeExists := false doDelete := func() error { - client := domains.NewGroupsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGroupsClient(context, connector) return client.Delete(d.Get("domain").(string), id, &failIfSubtreeExists, &forceDelete) } diff --git a/nsxt/resource_nsxt_policy_intrusion_service_policy.go b/nsxt/resource_nsxt_policy_intrusion_service_policy.go index 2525449fe..ff553b0c2 100644 --- a/nsxt/resource_nsxt_policy_intrusion_service_policy.go +++ b/nsxt/resource_nsxt_policy_intrusion_service_policy.go @@ -284,7 +284,11 @@ func updateIdsSecurityPolicy(id string, d *schema.ResourceData, m interface{}) e obj.Children = childRules } - return idsPolicyInfraPatch(getSessionContext(d, m), obj, domain, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + return idsPolicyInfraPatch(context, obj, domain, m) } func idsPolicyInfraPatch(context utl.SessionContext, policy model.IdsSecurityPolicy, domain string, m interface{}) error { @@ -309,7 +313,11 @@ func idsPolicyInfraPatch(context utl.SessionContext, policy model.IdsSecurityPol func resourceNsxtPolicyIntrusionServicePolicyCreate(d *schema.ResourceData, m interface{}) error { // Initialize resource Id and verify this ID is not yet used - id, err := getOrGenerateID2(d, m, resourceNsxtPolicyIntrusionServicePolicyExistsPartial(getSessionContext(d, m), d.Get("domain").(string))) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + id, err := getOrGenerateID2(d, m, resourceNsxtPolicyIntrusionServicePolicyExistsPartial(context, d.Get("domain").(string))) if err != nil { return err } @@ -334,7 +342,11 @@ func resourceNsxtPolicyIntrusionServicePolicyRead(d *schema.ResourceData, m inte if id == "" { return fmt.Errorf("Error obtaining Intrusion Service Policy id") } - client := domains.NewIntrusionServicePoliciesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewIntrusionServicePoliciesClient(context, connector) obj, err := client.Get(domainName, id) if err != nil { return handleReadError(d, "Intrusion Service Policy", id, err) @@ -378,8 +390,12 @@ func resourceNsxtPolicyIntrusionServicePolicyDelete(d *schema.ResourceData, m in connector := getPolicyConnector(m) - client := domains.NewIntrusionServicePoliciesClient(getSessionContext(d, m), connector) - err := client.Delete(d.Get("domain").(string), id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewIntrusionServicePoliciesClient(context, connector) + err = client.Delete(d.Get("domain").(string), id) if err != nil { return handleDeleteError("Intrusion Service Policy", id, err) diff --git a/nsxt/resource_nsxt_policy_intrusion_service_profile.go b/nsxt/resource_nsxt_policy_intrusion_service_profile.go index 8c58bdaa1..cc586a4c8 100644 --- a/nsxt/resource_nsxt_policy_intrusion_service_profile.go +++ b/nsxt/resource_nsxt_policy_intrusion_service_profile.go @@ -373,7 +373,11 @@ func resourceNsxtPolicyIntrusionServiceProfileCreate(d *schema.ResourceData, m i // Create the resource using PATCH log.Printf("[INFO] Creating Intrusion Service Profile with ID %s", id) - client := services.NewProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := services.NewProfilesClient(context, connector) err = client.Patch(id, obj) if err != nil { return handleCreateError("Ids Profile", id, err) @@ -393,7 +397,11 @@ func resourceNsxtPolicyIntrusionServiceProfileRead(d *schema.ResourceData, m int return fmt.Errorf("Error obtaining Ids Profile ID") } - client := services.NewProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := services.NewProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "Ids Profile", id, err) @@ -449,7 +457,11 @@ func resourceNsxtPolicyIntrusionServiceProfileUpdate(d *schema.ResourceData, m i // Create the resource using PATCH log.Printf("[INFO] Update Intrusion Service Profile with ID %s", id) - client := services.NewProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := services.NewProfilesClient(context, connector) err = client.Patch(id, obj) if err != nil { return handleUpdateError("Ids Profile", id, err) @@ -468,8 +480,11 @@ func resourceNsxtPolicyIntrusionServiceProfileDelete(d *schema.ResourceData, m i } connector := getPolicyConnector(m) - var err error - client := services.NewProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := services.NewProfilesClient(context, connector) err = client.Delete(id) if err != nil { diff --git a/nsxt/resource_nsxt_policy_ip_address_allocation.go b/nsxt/resource_nsxt_policy_ip_address_allocation.go index 324fb7a2f..cae9055bb 100644 --- a/nsxt/resource_nsxt_policy_ip_address_allocation.go +++ b/nsxt/resource_nsxt_policy_ip_address_allocation.go @@ -77,8 +77,11 @@ func resourceNsxtPolicyIPAddressAllocationExists(sessionContext utl.SessionConte func resourceNsxtPolicyIPAddressAllocationCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - sessionContext := getSessionContext(d, m) - client := ippools.NewIpAllocationsClient(sessionContext, connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpAllocationsClient(context, connector) if client == nil { return policyResourceNotSupportedError() @@ -92,7 +95,7 @@ func resourceNsxtPolicyIPAddressAllocationCreate(d *schema.ResourceData, m inter id = uuid.String() } - exists, err := resourceNsxtPolicyIPAddressAllocationExists(sessionContext, poolID, id, connector) + exists, err := resourceNsxtPolicyIPAddressAllocationExists(context, poolID, id, connector) if err != nil { return err } @@ -130,7 +133,11 @@ func resourceNsxtPolicyIPAddressAllocationCreate(d *schema.ResourceData, m inter func resourceNsxtPolicyIPAddressAllocationRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpAllocationsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpAllocationsClient(context, connector) if client == nil { return policyResourceNotSupportedError() @@ -182,7 +189,11 @@ func resourceNsxtPolicyIPAddressAllocationRead(d *schema.ResourceData, m interfa func resourceNsxtPolicyIPAddressAllocationUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpAllocationsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpAllocationsClient(context, connector) if client == nil { return policyResourceNotSupportedError() @@ -206,7 +217,7 @@ func resourceNsxtPolicyIPAddressAllocationUpdate(d *schema.ResourceData, m inter // Update the resource using PATCH log.Printf("[INFO] Updating IPAddressAllocation with ID %s", id) - err := client.Patch(poolID, id, obj) + err = client.Patch(poolID, id, obj) if err != nil { return handleUpdateError("IPAddressAllocation", id, err) } @@ -216,7 +227,11 @@ func resourceNsxtPolicyIPAddressAllocationUpdate(d *schema.ResourceData, m inter func resourceNsxtPolicyIPAddressAllocationDelete(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpAllocationsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpAllocationsClient(context, connector) if client == nil { return policyResourceNotSupportedError() } @@ -228,7 +243,7 @@ func resourceNsxtPolicyIPAddressAllocationDelete(d *schema.ResourceData, m inter poolID := getPolicyIDFromPath(d.Get("pool_path").(string)) - err := client.Delete(poolID, id) + err = client.Delete(poolID, id) if err != nil { return handleDeleteError("IPAddressAllocation", id, err) } @@ -260,7 +275,11 @@ func resourceNsxtPolicyIPAddressAllocationImport(d *schema.ResourceData, m inter poolID := s[0] connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + client := infra.NewIpPoolsClient(context, connector) pool, err := client.Get(poolID) if err != nil { diff --git a/nsxt/resource_nsxt_policy_ip_block.go b/nsxt/resource_nsxt_policy_ip_block.go index 7e9a24008..a5ac3d40c 100644 --- a/nsxt/resource_nsxt_policy_ip_block.go +++ b/nsxt/resource_nsxt_policy_ip_block.go @@ -73,7 +73,11 @@ func resourceNsxtPolicyIPBlockExists(sessionContext utl.SessionContext, id strin func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpBlocksClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpBlocksClient(context, connector) id := d.Id() if id == "" { @@ -99,7 +103,11 @@ func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpBlocksClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpBlocksClient(context, connector) id, err := getOrGenerateID2(d, m, resourceNsxtPolicyIPBlockExists) if err != nil { @@ -135,7 +143,11 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro func resourceNsxtPolicyIPBlockUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpBlocksClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpBlocksClient(context, connector) id := d.Id() if id == "" { @@ -162,7 +174,7 @@ func resourceNsxtPolicyIPBlockUpdate(d *schema.ResourceData, m interface{}) erro obj.Visibility = &visibility } - _, err := client.Update(id, obj) + _, err = client.Update(id, obj) if err != nil { return handleUpdateError("IP Block", id, err) } @@ -177,8 +189,12 @@ func resourceNsxtPolicyIPBlockDelete(d *schema.ResourceData, m interface{}) erro } connector := getPolicyConnector(m) - client := infra.NewIpBlocksClient(getSessionContext(d, m), connector) - err := client.Delete(id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpBlocksClient(context, connector) + err = client.Delete(id) if err != nil { return handleDeleteError("IP Block", id, err) } diff --git a/nsxt/resource_nsxt_policy_ip_discovery_profile.go b/nsxt/resource_nsxt_policy_ip_discovery_profile.go index dc2ed7aa8..94b30c284 100644 --- a/nsxt/resource_nsxt_policy_ip_discovery_profile.go +++ b/nsxt/resource_nsxt_policy_ip_discovery_profile.go @@ -182,7 +182,11 @@ func resourceNsxtPolicyIPDiscoveryProfileCreate(d *schema.ResourceData, m interf // Create the resource using PATCH log.Printf("[INFO] Creating IPDiscoveryProfile with ID %s", id) boolFalse := false - client := infra.NewIpDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpDiscoveryProfilesClient(context, connector) err = client.Patch(id, obj, &boolFalse) if err != nil { return handleCreateError("IPDiscoveryProfile", id, err) @@ -202,7 +206,11 @@ func resourceNsxtPolicyIPDiscoveryProfileRead(d *schema.ResourceData, m interfac return fmt.Errorf("Error obtaining IPDiscoveryProfile ID") } - client := infra.NewIpDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpDiscoveryProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "IPDiscoveryProfile", id, err) @@ -232,7 +240,11 @@ func resourceNsxtPolicyIPDiscoveryProfileRead(d *schema.ResourceData, m interfac func resourceNsxtPolicyIPDiscoveryProfileUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpDiscoveryProfilesClient(context, connector) if client == nil { return policyResourceNotSupportedError() } @@ -248,7 +260,7 @@ func resourceNsxtPolicyIPDiscoveryProfileUpdate(d *schema.ResourceData, m interf // Create the resource using PATCH log.Printf("[INFO] Updating IPDiscoveryProfile with ID %s", id) boolFalse := false - err := client.Patch(id, obj, &boolFalse) + err = client.Patch(id, obj, &boolFalse) if err != nil { return handleUpdateError("IPDiscoveryProfile", id, err) } @@ -266,7 +278,11 @@ func resourceNsxtPolicyIPDiscoveryProfileDelete(d *schema.ResourceData, m interf connector := getPolicyConnector(m) boolFalse := false - client := infra.NewIpDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpDiscoveryProfilesClient(context, connector) err = client.Delete(id, &boolFalse) if err != nil { diff --git a/nsxt/resource_nsxt_policy_ip_pool.go b/nsxt/resource_nsxt_policy_ip_pool.go index ad7c0cda4..d52b16a5f 100644 --- a/nsxt/resource_nsxt_policy_ip_pool.go +++ b/nsxt/resource_nsxt_policy_ip_pool.go @@ -59,7 +59,11 @@ func resourceNsxtPolicyIPPoolExists(sessionContext utl.SessionContext, id string func resourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpPoolsClient(context, connector) id := d.Id() if id == "" { @@ -89,7 +93,11 @@ func resourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error { func resourceNsxtPolicyIPPoolCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpPoolsClient(context, connector) id, err := getOrGenerateID2(d, m, resourceNsxtPolicyIPPoolExists) if err != nil { @@ -120,7 +128,11 @@ func resourceNsxtPolicyIPPoolCreate(d *schema.ResourceData, m interface{}) error func resourceNsxtPolicyIPPoolUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpPoolsClient(context, connector) id := d.Id() if id == "" { @@ -139,7 +151,7 @@ func resourceNsxtPolicyIPPoolUpdate(d *schema.ResourceData, m interface{}) error } log.Printf("[INFO] Updating IP Pool with ID %s", id) - err := client.Patch(id, obj) + err = client.Patch(id, obj) if err != nil { return handleUpdateError("IP Pool", id, err) } @@ -151,7 +163,11 @@ func resourceNsxtPolicyIPPoolUpdate(d *schema.ResourceData, m interface{}) error func resourceNsxtPolicyIPPoolDelete(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewIpPoolsClient(context, connector) id := d.Id() if id == "" { @@ -159,7 +175,7 @@ func resourceNsxtPolicyIPPoolDelete(d *schema.ResourceData, m interface{}) error } log.Printf("[INFO] Deleting IP Pool with ID %s", id) - err := client.Delete(id) + err = client.Delete(id) if err != nil { return handleDeleteError("IP Pool", id, err) } diff --git a/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go b/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go index adc522649..5e3dafafe 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go +++ b/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go @@ -92,7 +92,11 @@ func resourceNsxtPolicyIPPoolBlockSubnetSchemaToStructValue(d *schema.ResourceDa func resourceNsxtPolicyIPPoolBlockSubnetRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) converter := bindings.NewTypeConverter() poolPath := d.Get("pool_path").(string) @@ -135,7 +139,11 @@ func resourceNsxtPolicyIPPoolBlockSubnetRead(d *schema.ResourceData, m interface func resourceNsxtPolicyIPPoolBlockSubnetCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -170,7 +178,11 @@ func resourceNsxtPolicyIPPoolBlockSubnetCreate(d *schema.ResourceData, m interfa func resourceNsxtPolicyIPPoolBlockSubnetUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -198,7 +210,11 @@ func resourceNsxtPolicyIPPoolBlockSubnetUpdate(d *schema.ResourceData, m interfa func resourceNsxtPolicyIPPoolBlockSubnetDelete(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -209,12 +225,12 @@ func resourceNsxtPolicyIPPoolBlockSubnetDelete(d *schema.ResourceData, m interfa } log.Printf("[INFO] Deleting Block Subnet with ID %s", id) - err := client.Delete(poolID, id, nil) + err = client.Delete(poolID, id, nil) if err != nil { return handleDeleteError("Block Subnet", id, err) } - return resourceNsxtPolicyIPPoolBlockSubnetVerifyDelete(getSessionContext(d, m), d, connector) + return resourceNsxtPolicyIPPoolBlockSubnetVerifyDelete(context, d, connector) } // NOTE: This will not be needed when IPAM is handled by NSXT Policy @@ -275,7 +291,11 @@ func resourceNsxtPolicyIPPoolSubnetImport(d *schema.ResourceData, m interface{}) poolID := s[0] connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + client := infra.NewIpPoolsClient(context, connector) pool, err := client.Get(poolID) if err != nil { diff --git a/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go b/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go index 64d95df3a..426453230 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go +++ b/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go @@ -120,7 +120,11 @@ func resourceNsxtPolicyIPPoolStaticSubnetSchemaToStructValue(d *schema.ResourceD func resourceNsxtPolicyIPPoolStaticSubnetRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) converter := bindings.NewTypeConverter() poolPath := d.Get("pool_path").(string) @@ -173,7 +177,11 @@ func resourceNsxtPolicyIPPoolStaticSubnetRead(d *schema.ResourceData, m interfac func resourceNsxtPolicyIPPoolStaticSubnetCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -208,7 +216,11 @@ func resourceNsxtPolicyIPPoolStaticSubnetCreate(d *schema.ResourceData, m interf func resourceNsxtPolicyIPPoolStaticSubnetUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -236,7 +248,11 @@ func resourceNsxtPolicyIPPoolStaticSubnetUpdate(d *schema.ResourceData, m interf func resourceNsxtPolicyIPPoolStaticSubnetDelete(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := ippools.NewIpSubnetsClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := ippools.NewIpSubnetsClient(context, connector) poolPath := d.Get("pool_path").(string) poolID := getPolicyIDFromPath(poolPath) @@ -247,7 +263,7 @@ func resourceNsxtPolicyIPPoolStaticSubnetDelete(d *schema.ResourceData, m interf } log.Printf("[INFO] Deleting Static Subnet with ID %s", id) - err := client.Delete(poolID, id, nil) + err = client.Delete(poolID, id, nil) if err != nil { return handleDeleteError("Static Subnet", id, err) } diff --git a/nsxt/resource_nsxt_policy_mac_discovery_profile.go b/nsxt/resource_nsxt_policy_mac_discovery_profile.go index f2df7d2da..98a4d2c77 100644 --- a/nsxt/resource_nsxt_policy_mac_discovery_profile.go +++ b/nsxt/resource_nsxt_policy_mac_discovery_profile.go @@ -181,7 +181,11 @@ func resourceNsxtPolicyMacDiscoveryProfileCreate(d *schema.ResourceData, m inter // Create the resource using PATCH log.Printf("[INFO] Creating MacDiscoveryProfile with ID %s", id) boolFalse := false - client := infra.NewMacDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewMacDiscoveryProfilesClient(context, connector) err = client.Patch(id, obj, &boolFalse) if err != nil { return handleCreateError("MacDiscoveryProfile", id, err) @@ -201,7 +205,11 @@ func resourceNsxtPolicyMacDiscoveryProfileRead(d *schema.ResourceData, m interfa return fmt.Errorf("Error obtaining MacDiscoveryProfile ID") } - client := infra.NewMacDiscoveryProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewMacDiscoveryProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "MacDiscoveryProfile", id, err) @@ -247,8 +255,12 @@ func resourceNsxtPolicyMacDiscoveryProfileUpdate(d *schema.ResourceData, m inter // Update the resource using PATCH boolFalse := false - client := infra.NewMacDiscoveryProfilesClient(getSessionContext(d, m), connector) - _, err := client.Update(id, obj, &boolFalse) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewMacDiscoveryProfilesClient(context, connector) + _, err = client.Update(id, obj, &boolFalse) if err != nil { return handleUpdateError("MacDiscoveryProfile", id, err) } @@ -264,8 +276,12 @@ func resourceNsxtPolicyMacDiscoveryProfileDelete(d *schema.ResourceData, m inter connector := getPolicyConnector(m) boolFalse := false - client := infra.NewMacDiscoveryProfilesClient(getSessionContext(d, m), connector) - err := client.Delete(id, &boolFalse) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewMacDiscoveryProfilesClient(context, connector) + err = client.Delete(id, &boolFalse) if err != nil { return handleDeleteError("MacDiscoveryProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_nat_rule.go b/nsxt/resource_nsxt_policy_nat_rule.go index f4285dcc2..2758e8e00 100644 --- a/nsxt/resource_nsxt_policy_nat_rule.go +++ b/nsxt/resource_nsxt_policy_nat_rule.go @@ -168,14 +168,17 @@ func resourceNsxtPolicyNATRuleDelete(d *schema.ResourceData, m interface{}) erro if gwID == "" { return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } action := d.Get("action").(string) natType := getNatTypeByAction(action) - err := deleteNsxtPolicyNATRule(context, getPolicyConnector(m), gwID, isT0, natType, id) + err = deleteNsxtPolicyNATRule(context, getPolicyConnector(m), gwID, isT0, natType, id) if err != nil { return handleDeleteError("NAT Rule", id, err) } @@ -261,7 +264,10 @@ func resourceNsxtPolicyNATRuleRead(d *schema.ResourceData, m interface{}) error return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -315,7 +321,10 @@ func resourceNsxtPolicyNATRuleCreate(d *schema.ResourceData, m interface{}) erro return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -376,7 +385,10 @@ func resourceNsxtPolicyNATRuleCreate(d *schema.ResourceData, m interface{}) erro log.Printf("[INFO] Creating NAT Rule with ID %s", id) - err := patchNsxtPolicyNATRule(getSessionContext(d, m), connector, gwID, ruleStruct, isT0) + if err != nil { + return err + } + err = patchNsxtPolicyNATRule(context, connector, gwID, ruleStruct, isT0) if err != nil { return handleCreateError("NAT Rule", id, err) } @@ -400,7 +412,10 @@ func resourceNsxtPolicyNATRuleUpdate(d *schema.ResourceData, m interface{}) erro if gwID == "" { return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -449,7 +464,7 @@ func resourceNsxtPolicyNATRuleUpdate(d *schema.ResourceData, m interface{}) erro } log.Printf("[INFO] Updating NAT Rule with ID %s", id) - err := patchNsxtPolicyNATRule(context, connector, gwID, ruleStruct, isT0) + err = patchNsxtPolicyNATRule(context, connector, gwID, ruleStruct, isT0) if err != nil { return handleUpdateError("NAT Rule", id, err) } @@ -496,13 +511,17 @@ func resourceNsxtPolicyNATRuleImport(d *schema.ResourceData, m interface{}) ([]* gwID := s[0] connector := getPolicyConnector(m) - t0Client := infra.NewTier0sClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + t0Client := infra.NewTier0sClient(context, connector) t0gw, err := t0Client.Get(gwID) if err != nil { if !isNotFoundError(err) { return nil, err } - t1Client := infra.NewTier1sClient(getSessionContext(d, m), connector) + t1Client := infra.NewTier1sClient(context, connector) t1gw, err := t1Client.Get(gwID) if err != nil { return nil, err diff --git a/nsxt/resource_nsxt_policy_ospf_config.go b/nsxt/resource_nsxt_policy_ospf_config.go index 11d7677c5..c382996e3 100644 --- a/nsxt/resource_nsxt_policy_ospf_config.go +++ b/nsxt/resource_nsxt_policy_ospf_config.go @@ -142,7 +142,11 @@ func resourceNsxtPolicyOspfConfigCreate(d *schema.ResourceData, m interface{}) e return fmt.Errorf("Tier0 Gateway path expected, got %s", gwPath) } - localeService, err := getPolicyTier0GatewayLocaleServiceWithEdgeCluster(getSessionContext(d, m), gwID, connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + localeService, err := getPolicyTier0GatewayLocaleServiceWithEdgeCluster(context, gwID, connector) if err != nil { return fmt.Errorf("Tier0 Gateway path with configured edge cluster expected, got %s", gwPath) } diff --git a/nsxt/resource_nsxt_policy_parent_security_policy.go b/nsxt/resource_nsxt_policy_parent_security_policy.go index 24a610656..571a6fe83 100644 --- a/nsxt/resource_nsxt_policy_parent_security_policy.go +++ b/nsxt/resource_nsxt_policy_parent_security_policy.go @@ -62,7 +62,11 @@ func parentSecurityPolicyModelToSchema(d *schema.ResourceData, m interface{}) (* if id == "" { return nil, fmt.Errorf("Error obtaining Security Policy id") } - client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + client := domains.NewSecurityPoliciesClient(context, connector) obj, err := client.Get(domainName, id) if err != nil { return nil, err diff --git a/nsxt/resource_nsxt_policy_predefined_gateway_policy.go b/nsxt/resource_nsxt_policy_predefined_gateway_policy.go index 4e9a9b3fe..f324e9b61 100644 --- a/nsxt/resource_nsxt_policy_predefined_gateway_policy.go +++ b/nsxt/resource_nsxt_policy_predefined_gateway_policy.go @@ -293,7 +293,11 @@ func updatePolicyPredefinedGatewayPolicy(id string, d *schema.ResourceData, m in return fmt.Errorf("Failed to extract domain from Gateway Policy path %s", path) } - predefinedPolicy, err := getGatewayPolicyInDomain(getSessionContext(d, m), id, domain, connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + predefinedPolicy, err := getGatewayPolicyInDomain(context, id, domain, connector) if err != nil { return err } @@ -376,7 +380,10 @@ func updatePolicyPredefinedGatewayPolicy(id string, d *schema.ResourceData, m in predefinedPolicy.Children = childRules } - err = gatewayPolicyInfraPatch(getSessionContext(d, m), predefinedPolicy, domain, m) + if err != nil { + return err + } + err = gatewayPolicyInfraPatch(context, predefinedPolicy, domain, m) if err != nil { return handleUpdateError("Predefined Gateway Policy", id, err) } @@ -413,7 +420,11 @@ func resourceNsxtPolicyPredefinedGatewayPolicyRead(d *schema.ResourceData, m int path := d.Get("path").(string) domain := getDomainFromResourcePath(path) - client := domains.NewGatewayPoliciesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewGatewayPoliciesClient(context, connector) obj, err := client.Get(domain, id) if err != nil { return handleReadError(d, "Predefined Gateway Policy", id, err) @@ -465,7 +476,11 @@ func resourceNsxtPolicyPredefinedGatewayPolicyDelete(d *schema.ResourceData, m i path := d.Get("path").(string) domain := getDomainFromResourcePath(path) - predefinedPolicy, err := getGatewayPolicyInDomain(getSessionContext(d, m), id, domain, getPolicyConnector(m)) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + predefinedPolicy, err := getGatewayPolicyInDomain(context, id, domain, getPolicyConnector(m)) if err != nil { return err } @@ -475,7 +490,7 @@ func resourceNsxtPolicyPredefinedGatewayPolicyDelete(d *schema.ResourceData, m i return fmt.Errorf("Failed to revert Predefined Gateway Policy %s: %s", id, err) } - err = gatewayPolicyInfraPatch(getSessionContext(d, m), revertedPolicy, domain, m) + err = gatewayPolicyInfraPatch(context, revertedPolicy, domain, m) if err != nil { return handleUpdateError("Predefined Gateway Policy", id, err) } diff --git a/nsxt/resource_nsxt_policy_predefined_security_policy.go b/nsxt/resource_nsxt_policy_predefined_security_policy.go index 0fcc65fe1..1b94aadf6 100644 --- a/nsxt/resource_nsxt_policy_predefined_security_policy.go +++ b/nsxt/resource_nsxt_policy_predefined_security_policy.go @@ -219,7 +219,11 @@ func updatePolicyPredefinedSecurityPolicy(id string, d *schema.ResourceData, m i return fmt.Errorf("Failed to extract domain from Security Policy path %s", path) } - predefinedPolicy, err := getSecurityPolicyInDomain(getSessionContext(d, m), id, domain, connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + predefinedPolicy, err := getSecurityPolicyInDomain(context, id, domain, connector) if err != nil { return err } @@ -298,7 +302,10 @@ func updatePolicyPredefinedSecurityPolicy(id string, d *schema.ResourceData, m i predefinedPolicy.Children = childRules } - err = securityPolicyInfraPatch(getSessionContext(d, m), predefinedPolicy, domain, m) + if err != nil { + return err + } + err = securityPolicyInfraPatch(context, predefinedPolicy, domain, m) if err != nil { return handleUpdateError("Predefined Security Policy", id, err) } @@ -335,7 +342,11 @@ func resourceNsxtPolicyPredefinedSecurityPolicyRead(d *schema.ResourceData, m in path := d.Get("path").(string) domain := getDomainFromResourcePath(path) - client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewSecurityPoliciesClient(context, connector) obj, err := client.Get(domain, id) if err != nil { return handleReadError(d, "Predefined Security Policy", id, err) @@ -386,7 +397,10 @@ func resourceNsxtPolicyPredefinedSecurityPolicyDelete(d *schema.ResourceData, m path := d.Get("path").(string) domain := getDomainFromResourcePath(path) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } predefinedPolicy, err := getSecurityPolicyInDomain(context, id, domain, getPolicyConnector(m)) if err != nil { diff --git a/nsxt/resource_nsxt_policy_qos_profile.go b/nsxt/resource_nsxt_policy_qos_profile.go index d58dbb043..60f8a45cb 100644 --- a/nsxt/resource_nsxt_policy_qos_profile.go +++ b/nsxt/resource_nsxt_policy_qos_profile.go @@ -174,7 +174,11 @@ func resourceNsxtPolicyQosProfileCreate(d *schema.ResourceData, m interface{}) e // Create the resource using PATCH log.Printf("[INFO] Creating QosProfile with ID %s", id) boolFalse := false - client := infra.NewQosProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewQosProfilesClient(context, connector) err = client.Patch(id, obj, &boolFalse) if err != nil { return handleCreateError("QosProfile", id, err) @@ -194,7 +198,11 @@ func resourceNsxtPolicyQosProfileRead(d *schema.ResourceData, m interface{}) err return fmt.Errorf("Error obtaining QosProfile ID") } - client := infra.NewQosProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewQosProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "QosProfile", id, err) @@ -222,7 +230,11 @@ func resourceNsxtPolicyQosProfileRead(d *schema.ResourceData, m interface{}) err func resourceNsxtPolicyQosProfileUpdate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - client := infra.NewQosProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewQosProfilesClient(context, connector) if client == nil { return policyResourceNotSupportedError() } @@ -267,7 +279,7 @@ func resourceNsxtPolicyQosProfileUpdate(d *schema.ResourceData, m interface{}) e // Create the resource using PATCH log.Printf("[INFO] Updating QosProfile with ID %s", id) boolFalse := false - err := client.Patch(id, obj, &boolFalse) + err = client.Patch(id, obj, &boolFalse) if err != nil { d.Partial(true) return handleUpdateError("QosProfile", id, err) @@ -284,8 +296,12 @@ func resourceNsxtPolicyQosProfileDelete(d *schema.ResourceData, m interface{}) e connector := getPolicyConnector(m) boolFalse := false - client := infra.NewQosProfilesClient(getSessionContext(d, m), connector) - err := client.Delete(id, &boolFalse) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewQosProfilesClient(context, connector) + err = client.Delete(id, &boolFalse) if err != nil { return handleDeleteError("QosProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_security_policy.go b/nsxt/resource_nsxt_policy_security_policy.go index 7792712c9..15cb53913 100644 --- a/nsxt/resource_nsxt_policy_security_policy.go +++ b/nsxt/resource_nsxt_policy_security_policy.go @@ -82,7 +82,11 @@ func policySecurityPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, id } log.Printf("[INFO] Using selective H-API for policy with ID %s", id) - return securityPolicyInfraPatch(getSessionContext(d, m), obj, domain, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + return securityPolicyInfraPatch(context, obj, domain, m) } func resourceNsxtPolicySecurityPolicyCreate(d *schema.ResourceData, m interface{}) error { @@ -105,8 +109,12 @@ func resourceNsxtPolicySecurityPolicyDelete(d *schema.ResourceData, m interface{ connector := getPolicyConnector(m) - client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector) - err := client.Delete(d.Get("domain").(string), id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := domains.NewSecurityPoliciesClient(context, connector) + err = client.Delete(d.Get("domain").(string), id) if err != nil { return handleDeleteError("Security Policy", id, err) diff --git a/nsxt/resource_nsxt_policy_security_policy_rule.go b/nsxt/resource_nsxt_policy_security_policy_rule.go index 8a99512e6..cfd7688a9 100644 --- a/nsxt/resource_nsxt_policy_security_policy_rule.go +++ b/nsxt/resource_nsxt_policy_security_policy_rule.go @@ -48,7 +48,11 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf } log.Printf("[INFO] Creating Security Policy Rule with ID %s under policy %s", id, policyPath) - client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := securitypolicies.NewRulesClient(context, connector) rule := securityPolicyRuleSchemaToModel(d, id) err = client.Patch(domain, policyID, id, rule) if err != nil { @@ -62,7 +66,10 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf } func setSecurityPolicyRuleContext(d *schema.ResourceData, m interface{}, projectID string) error { - providedProjectID, _ := getContextDataFromSchema(d, m) + providedProjectID, _, err := getContextDataFromSchema(d, m) + if err != nil { + return err + } if providedProjectID == "" { contexts := make([]interface{}, 1) ctxMap := make(map[string]interface{}) @@ -161,7 +168,11 @@ func resourceNsxtPolicySecurityPolicyRuleRead(d *schema.ResourceData, m interfac return handleReadError(d, "SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err) } - client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := securitypolicies.NewRulesClient(context, connector) rule, err := client.Get(domain, policyID, id) if err != nil { return handleReadError(d, "SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err) @@ -214,9 +225,13 @@ func resourceNsxtPolicySecurityPolicyRuleUpdate(d *schema.ResourceData, m interf domain := getDomainFromResourcePath(policyPath) policyID := getPolicyIDFromPath(policyPath) - client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := securitypolicies.NewRulesClient(context, connector) rule := securityPolicyRuleSchemaToModel(d, id) - err := client.Patch(domain, policyID, id, rule) + err = client.Patch(domain, policyID, id, rule) if err != nil { return handleUpdateError("SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err) } @@ -237,7 +252,11 @@ func resourceNsxtPolicySecurityPolicyRuleDelete(d *schema.ResourceData, m interf domain := getDomainFromResourcePath(policyPath) policyID := getPolicyIDFromPath(policyPath) - client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := securitypolicies.NewRulesClient(context, connector) return client.Delete(domain, policyID, id) } diff --git a/nsxt/resource_nsxt_policy_segment_security_profile.go b/nsxt/resource_nsxt_policy_segment_security_profile.go index a12b95d63..d91b79538 100644 --- a/nsxt/resource_nsxt_policy_segment_security_profile.go +++ b/nsxt/resource_nsxt_policy_segment_security_profile.go @@ -287,7 +287,11 @@ func resourceNsxtPolicySegmentSecurityProfilePatch(d *schema.ResourceData, m int } log.Printf("[INFO] Sending SegmentSecurityProfile with ID %s", id) - client := infra.NewSegmentSecurityProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSegmentSecurityProfilesClient(context, connector) return client.Patch(id, obj, nil) } @@ -318,7 +322,11 @@ func resourceNsxtPolicySegmentSecurityProfileRead(d *schema.ResourceData, m inte return fmt.Errorf("Error obtaining SegmentSecurityProfile ID") } - client := infra.NewSegmentSecurityProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSegmentSecurityProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "SegmentSecurityProfile", id, err) @@ -361,8 +369,12 @@ func resourceNsxtPolicySegmentSecurityProfileDelete(d *schema.ResourceData, m in } connector := getPolicyConnector(m) - client := infra.NewSegmentSecurityProfilesClient(getSessionContext(d, m), connector) - err := client.Delete(id, nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSegmentSecurityProfilesClient(context, connector) + err = client.Delete(id, nil) if err != nil { return handleDeleteError("SegmentSecurityProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_service.go b/nsxt/resource_nsxt_policy_service.go index bf516cf2a..67df4276d 100644 --- a/nsxt/resource_nsxt_policy_service.go +++ b/nsxt/resource_nsxt_policy_service.go @@ -470,7 +470,11 @@ func resourceNsxtPolicyServiceCreate(d *schema.ResourceData, m interface{}) erro // Create the resource using PATCH log.Printf("[INFO] Creating service with ID %s", id) - client := infra.NewServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewServicesClient(context, connector) err = client.Patch(id, obj) if err != nil { return handleCreateError("Service", id, err) @@ -489,7 +493,11 @@ func resourceNsxtPolicyServiceRead(d *schema.ResourceData, m interface{}) error return fmt.Errorf("Error obtaining service id") } - client := infra.NewServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewServicesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "Service", id, err) @@ -680,8 +688,12 @@ func resourceNsxtPolicyServiceUpdate(d *schema.ResourceData, m interface{}) erro } // Update the resource using Update to totally replace the list of entries - client := infra.NewServicesClient(getSessionContext(d, m), connector) - _, err := client.Update(id, obj) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewServicesClient(context, connector) + _, err = client.Update(id, obj) if err != nil { return handleUpdateError("Service", id, err) @@ -698,7 +710,11 @@ func resourceNsxtPolicyServiceDelete(d *schema.ResourceData, m interface{}) erro connector := getPolicyConnector(m) doDelete := func() error { - client := infra.NewServicesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewServicesClient(context, connector) return client.Delete(id) } diff --git a/nsxt/resource_nsxt_policy_spoof_guard_profile.go b/nsxt/resource_nsxt_policy_spoof_guard_profile.go index 9184c35a5..68969b7eb 100644 --- a/nsxt/resource_nsxt_policy_spoof_guard_profile.go +++ b/nsxt/resource_nsxt_policy_spoof_guard_profile.go @@ -71,7 +71,11 @@ func resourceNsxtPolicySpoofGuardProfilePatch(d *schema.ResourceData, m interfac } log.Printf("[INFO] Patching SpoofGuardProfile with ID %s", id) - client := infra.NewSpoofguardProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSpoofguardProfilesClient(context, connector) return client.Patch(id, obj, nil) } @@ -102,7 +106,11 @@ func resourceNsxtPolicySpoofGuardProfileRead(d *schema.ResourceData, m interface return fmt.Errorf("Error obtaining SpoofGuardProfile ID") } - client := infra.NewSpoofguardProfilesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSpoofguardProfilesClient(context, connector) obj, err := client.Get(id) if err != nil { return handleReadError(d, "SpoofGuardProfile", id, err) @@ -142,8 +150,12 @@ func resourceNsxtPolicySpoofGuardProfileDelete(d *schema.ResourceData, m interfa } connector := getPolicyConnector(m) - client := infra.NewSpoofguardProfilesClient(getSessionContext(d, m), connector) - err := client.Delete(id, nil) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := infra.NewSpoofguardProfilesClient(context, connector) + err = client.Delete(id, nil) if err != nil { return handleDeleteError("SpoofGuardProfile", id, err) diff --git a/nsxt/resource_nsxt_policy_static_route.go b/nsxt/resource_nsxt_policy_static_route.go index b147d4862..dad703e28 100644 --- a/nsxt/resource_nsxt_policy_static_route.go +++ b/nsxt/resource_nsxt_policy_static_route.go @@ -114,7 +114,10 @@ func resourceNsxtPolicyStaticRouteCreate(d *schema.ResourceData, m interface{}) if gwID == "" { return fmt.Errorf("gateway_path is not a valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -171,7 +174,10 @@ func resourceNsxtPolicyStaticRouteCreate(d *schema.ResourceData, m interface{}) } log.Printf("[INFO] Creating Static Route with ID %s", id) - err := patchNsxtPolicyStaticRoute(getSessionContext(d, m), connector, gwID, routeStruct, isT0) + if err != nil { + return err + } + err = patchNsxtPolicyStaticRoute(context, connector, gwID, routeStruct, isT0) if err != nil { return handleCreateError("Static Route", id, err) } @@ -195,7 +201,10 @@ func resourceNsxtPolicyStaticRouteRead(d *schema.ResourceData, m interface{}) er if gwID == "" { return fmt.Errorf("gateway_path is not a valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -253,7 +262,10 @@ func resourceNsxtPolicyStaticRouteUpdate(d *schema.ResourceData, m interface{}) if gwID == "" { return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } @@ -297,7 +309,7 @@ func resourceNsxtPolicyStaticRouteUpdate(d *schema.ResourceData, m interface{}) } log.Printf("[INFO] Updating Static Route with ID %s", id) - err := patchNsxtPolicyStaticRoute(context, connector, gwID, routeStruct, isT0) + err = patchNsxtPolicyStaticRoute(context, connector, gwID, routeStruct, isT0) if err != nil { return handleUpdateError("Static Route", id, err) } @@ -319,12 +331,15 @@ func resourceNsxtPolicyStaticRouteDelete(d *schema.ResourceData, m interface{}) if gwID == "" { return fmt.Errorf("gateway_path is not valid") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isT0 && context.ClientType == utl.Multitenancy { return handleMultitenancyTier0Error() } - err := deleteNsxtPolicyStaticRoute(context, getPolicyConnector(m), gwID, isT0, id) + err = deleteNsxtPolicyStaticRoute(context, getPolicyConnector(m), gwID, isT0, id) if err != nil { return handleDeleteError("Static Route", id, err) } @@ -352,13 +367,17 @@ func resourceNsxtPolicyStaticRouteImport(d *schema.ResourceData, m interface{}) gwID := s[0] connector := getPolicyConnector(m) - t0Client := infra.NewTier0sClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + t0Client := infra.NewTier0sClient(context, connector) t0gw, err := t0Client.Get(gwID) if err != nil { if !isNotFoundError(err) { return nil, err } - t1Client := infra.NewTier1sClient(getSessionContext(d, m), connector) + t1Client := infra.NewTier1sClient(context, connector) t1gw, err := t1Client.Get(gwID) if err != nil { return nil, err diff --git a/nsxt/resource_nsxt_policy_tier0_gateway.go b/nsxt/resource_nsxt_policy_tier0_gateway.go index 66eaa82e1..84f764e26 100644 --- a/nsxt/resource_nsxt_policy_tier0_gateway.go +++ b/nsxt/resource_nsxt_policy_tier0_gateway.go @@ -851,9 +851,12 @@ func policyTier0GatewayResourceToInfraStruct(context utl.SessionContext, d *sche func resourceNsxtPolicyTier0GatewayCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } isGlobalManager := context.ClientType == utl.Global - err := verifyPolicyTier0GatewayConfig(d, isGlobalManager) + err = verifyPolicyTier0GatewayConfig(d, isGlobalManager) if err != nil { return err } @@ -864,14 +867,17 @@ func resourceNsxtPolicyTier0GatewayCreate(d *schema.ResourceData, m interface{}) return err } - obj, err := policyTier0GatewayResourceToInfraStruct(getSessionContext(d, m), d, connector, id) + obj, err := policyTier0GatewayResourceToInfraStruct(context, d, connector, id) if err != nil { return err } log.Printf("[INFO] Using H-API to create Tier0 with ID %s", id) - err = policyInfraPatch(getSessionContext(d, m), obj, getPolicyConnector(m), false) + if err != nil { + return err + } + err = policyInfraPatch(context, obj, getPolicyConnector(m), false) if err != nil { return handleCreateError("Tier0", id, err) } @@ -942,7 +948,11 @@ func resourceNsxtPolicyTier0GatewayRead(d *schema.ResourceData, m interface{}) e d.Set("dhcp_config_path", dhcpPaths[0]) } // Get the edge cluster Id or locale services - localeServices, err := listPolicyTier0GatewayLocaleServices(getSessionContext(d, m), connector, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + localeServices, err := listPolicyTier0GatewayLocaleServices(context, connector, id) if err != nil { return handleReadError(d, "Locale Service for T0", id, err) } @@ -1039,14 +1049,18 @@ func resourceNsxtPolicyTier0GatewayUpdate(d *schema.ResourceData, m interface{}) return fmt.Errorf("Error obtaining Tier0 ID") } - obj, err := policyTier0GatewayResourceToInfraStruct(getSessionContext(d, m), d, connector, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyTier0GatewayResourceToInfraStruct(context, d, connector, id) if err != nil { return handleUpdateError("Tier0", id, err) } log.Printf("[INFO] Using H-API to update Tier0 with ID %s", id) - err = policyInfraPatch(getSessionContext(d, m), obj, connector, true) + err = policyInfraPatch(context, obj, connector, true) if err != nil { return handleUpdateError("Tier0", id, err) } @@ -1087,7 +1101,11 @@ func resourceNsxtPolicyTier0GatewayDelete(d *schema.ResourceData, m interface{}) } log.Printf("[DEBUG] Using H-API to delete Tier0 with ID %s", id) - err := policyInfraPatch(getSessionContext(d, m), obj, getPolicyConnector(m), false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + err = policyInfraPatch(context, obj, getPolicyConnector(m), false) if err != nil { return handleDeleteError("Tier0", id, err) } diff --git a/nsxt/resource_nsxt_policy_tier0_gateway_interface.go b/nsxt/resource_nsxt_policy_tier0_gateway_interface.go index 04db4a046..49c2a635f 100644 --- a/nsxt/resource_nsxt_policy_tier0_gateway_interface.go +++ b/nsxt/resource_nsxt_policy_tier0_gateway_interface.go @@ -248,7 +248,10 @@ func resourceNsxtPolicyTier0GatewayInterfaceCreate(d *schema.ResourceData, m int } localeServiceID := "" - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isPolicyGlobalManager(m) { enablePIM := d.Get("enable_pim").(bool) if enablePIM { @@ -330,7 +333,7 @@ func resourceNsxtPolicyTier0GatewayInterfaceCreate(d *schema.ResourceData, m int obj.EdgePath = &edgePath } - err := gatewayInterfaceVersionDepenantSet(d, m, &obj) + err = gatewayInterfaceVersionDepenantSet(d, m, &obj) if err != nil { return handleCreateError("Tier0 Interface", id, err) } diff --git a/nsxt/resource_nsxt_policy_tier1_gateway.go b/nsxt/resource_nsxt_policy_tier1_gateway.go index a4616c7fb..aff7e9691 100644 --- a/nsxt/resource_nsxt_policy_tier1_gateway.go +++ b/nsxt/resource_nsxt_policy_tier1_gateway.go @@ -502,7 +502,11 @@ func resourceNsxtPolicyTier1GatewayCreate(d *schema.ResourceData, m interface{}) return err } - obj, err := policyTier1GatewayResourceToInfraStruct(getSessionContext(d, m), d, connector, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyTier1GatewayResourceToInfraStruct(context, d, connector, id) if err != nil { return err @@ -510,7 +514,7 @@ func resourceNsxtPolicyTier1GatewayCreate(d *schema.ResourceData, m interface{}) // Create the resource using PATCH log.Printf("[INFO] Using H-API to create Tier1 with ID %s", id) - err = policyInfraPatch(getSessionContext(d, m), obj, connector, false) + err = policyInfraPatch(context, obj, connector, false) if err != nil { return handleCreateError("Tier1", id, err) } @@ -529,7 +533,10 @@ func resourceNsxtPolicyTier1GatewayRead(d *schema.ResourceData, m interface{}) e return fmt.Errorf("Error obtaining Tier1 id") } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } client := infra.NewTier1sClient(context, connector) obj, err := client.Get(id) @@ -652,13 +659,17 @@ func resourceNsxtPolicyTier1GatewayUpdate(d *schema.ResourceData, m interface{}) return fmt.Errorf("Error obtaining Tier1 id") } - obj, err := policyTier1GatewayResourceToInfraStruct(getSessionContext(d, m), d, connector, id) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policyTier1GatewayResourceToInfraStruct(context, d, connector, id) if err != nil { return err } log.Printf("[INFO] Using H-API to update Tier1 with ID %s", id) - err = policyInfraPatch(getSessionContext(d, m), obj, connector, true) + err = policyInfraPatch(context, obj, connector, true) if err != nil { return handleUpdateError("Tier1", id, err) } @@ -697,7 +708,11 @@ func resourceNsxtPolicyTier1GatewayDelete(d *schema.ResourceData, m interface{}) } log.Printf("[DEBUG] Using H-API to delete Tier1 with ID %s", id) - err := policyInfraPatch(getSessionContext(d, m), obj, getPolicyConnector(m), false) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + err = policyInfraPatch(context, obj, getPolicyConnector(m), false) if err != nil { return handleDeleteError("Tier1", id, err) } diff --git a/nsxt/resource_nsxt_policy_tier1_gateway_interface.go b/nsxt/resource_nsxt_policy_tier1_gateway_interface.go index 88a9861d0..2ed4d2189 100644 --- a/nsxt/resource_nsxt_policy_tier1_gateway_interface.go +++ b/nsxt/resource_nsxt_policy_tier1_gateway_interface.go @@ -88,7 +88,10 @@ func resourceNsxtPolicyTier1GatewayInterfaceCreate(d *schema.ResourceData, m int sitePath := d.Get("site_path").(string) tier1ID := getPolicyIDFromPath(tier1Path) localeServiceID := "" - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } if isPolicyGlobalManager(m) { if sitePath == "" { return attributeRequiredGlobalManagerError("site_path", "nsxt_policy_tier1_gateway_interface") @@ -120,7 +123,7 @@ func resourceNsxtPolicyTier1GatewayInterfaceCreate(d *schema.ResourceData, m int id = newUUID() } else { var err error - client := localeservices.NewInterfacesClient(getSessionContext(d, m), connector) + client := localeservices.NewInterfacesClient(context, connector) _, err = client.Get(tier1ID, localeServiceID, id) if err == nil { @@ -161,8 +164,8 @@ func resourceNsxtPolicyTier1GatewayInterfaceCreate(d *schema.ResourceData, m int // Create the resource using PATCH log.Printf("[INFO] Creating tier1 interface with ID %s", id) - client := localeservices.NewInterfacesClient(getSessionContext(d, m), connector) - err := client.Patch(tier1ID, localeServiceID, id, obj) + client := localeservices.NewInterfacesClient(context, connector) + err = client.Patch(tier1ID, localeServiceID, id, obj) if err != nil { return handleCreateError("Tier1 Interface", id, err) @@ -206,7 +209,11 @@ func resourceNsxtPolicyTier1GatewayInterfaceRead(d *schema.ResourceData, m inter d.Set("site_path", sitePath) } else { var err error - client := localeservices.NewInterfacesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := localeservices.NewInterfacesClient(context, connector) obj, err = client.Get(tier1ID, localeServiceID, id) if err != nil { return handleReadError(d, "Tier1 Interface", id, err) @@ -285,7 +292,11 @@ func resourceNsxtPolicyTier1GatewayInterfaceUpdate(d *schema.ResourceData, m int obj.UrpfMode = &urpfMode } var err error - client := localeservices.NewInterfacesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := localeservices.NewInterfacesClient(context, connector) _, err = client.Update(tier1ID, localeServiceID, id, obj) if err != nil { return handleUpdateError("Tier1 Interface", id, err) @@ -306,7 +317,11 @@ func resourceNsxtPolicyTier1GatewayInterfaceDelete(d *schema.ResourceData, m int } var err error - client := localeservices.NewInterfacesClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := localeservices.NewInterfacesClient(context, connector) err = client.Delete(tier1ID, localeServiceID, id) if err != nil { return handleDeleteError("Tier1 Interface", id, err) @@ -342,7 +357,11 @@ func resourceNsxtPolicyTier1GatewayInterfaceImport(d *schema.ResourceData, m int gwID := s[0] connector := getPolicyConnector(m) var tier1GW model.Tier1 - client := infra.NewTier1sClient(getSessionContext(d, m), connector) + context, err := getSessionContext(d, m) + if err != nil { + return nil, err + } + client := infra.NewTier1sClient(context, connector) tier1GW, err = client.Get(gwID) if err != nil { return nil, err diff --git a/nsxt/resource_nsxt_policy_vm_tags.go b/nsxt/resource_nsxt_policy_vm_tags.go index 88f8c76d5..a287f67df 100644 --- a/nsxt/resource_nsxt_policy_vm_tags.go +++ b/nsxt/resource_nsxt_policy_vm_tags.go @@ -335,7 +335,10 @@ func setPolicyVMPortTagsInSchema(d *schema.ResourceData, m interface{}, external if err != nil { return err } - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } portTags := d.Get("port").([]interface{}) var actualPortTags []map[string]interface{} @@ -376,7 +379,11 @@ func resourceNsxtPolicyVMTagsRead(d *schema.ResourceData, m interface{}) error { return fmt.Errorf("Error obtaining Virtual Machine ID") } - vm, err := findNsxtPolicyVMByID(getSessionContext(d, m), connector, vmID, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + vm, err := findNsxtPolicyVMByID(context, connector, vmID, m) if err != nil { d.SetId("") log.Printf("[ERROR] Cannot find VM with ID %s, skip reading VM tag", vmID) @@ -396,7 +403,10 @@ func resourceNsxtPolicyVMTagsRead(d *schema.ResourceData, m interface{}) error { func resourceNsxtPolicyVMTagsCreate(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) instanceID := d.Get("instance_id").(string) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } vm, err := findNsxtPolicyVMByID(context, connector, instanceID, m) if err != nil { @@ -439,7 +449,10 @@ func resourceNsxtPolicyVMTagsUpdate(d *schema.ResourceData, m interface{}) error func resourceNsxtPolicyVMTagsDelete(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) instanceID := d.Get("instance_id").(string) - context := getSessionContext(d, m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } vm, err := findNsxtPolicyVMByID(context, connector, instanceID, m) diff --git a/nsxt/segment_common.go b/nsxt/segment_common.go index bba9b45ec..6b64d46fd 100644 --- a/nsxt/segment_common.go +++ b/nsxt/segment_common.go @@ -1171,8 +1171,8 @@ func nsxtPolicySegmentDiscoveryProfileRead(d *schema.ResourceData, m interface{} } results = lmResults.(model.SegmentDiscoveryProfileBindingMapListResult) } else { - client := segments.NewSegmentDiscoveryProfileBindingMapsClient(getSessionContext(d, m), connector) - var err error + context, err := getSessionContext(d, m) + client := segments.NewSegmentDiscoveryProfileBindingMapsClient(context, connector) results, err = client.List(segmentID, nil, nil, nil, nil, nil, nil) if err != nil { return fmt.Errorf(errorMessage, segmentID, err) @@ -1212,8 +1212,11 @@ func nsxtPolicySegmentQosProfileRead(d *schema.ResourceData, m interface{}) erro } results = lmResults.(model.SegmentQosProfileBindingMapListResult) } else { - client := segments.NewSegmentQosProfileBindingMapsClient(getSessionContext(d, m), connector) - var err error + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := segments.NewSegmentQosProfileBindingMapsClient(context, connector) results, err = client.List(segmentID, nil, nil, nil, nil, nil) if err != nil { return fmt.Errorf(errorMessage, segmentID, err) @@ -1254,8 +1257,11 @@ func nsxtPolicySegmentSecurityProfileRead(d *schema.ResourceData, m interface{}) } results = lmResults.(model.SegmentSecurityProfileBindingMapListResult) } else { - client := segments.NewSegmentSecurityProfileBindingMapsClient(getSessionContext(d, m), connector) - var err error + context, err := getSessionContext(d, m) + if err != nil { + return err + } + client := segments.NewSegmentSecurityProfileBindingMapsClient(context, connector) results, err = client.List(segmentID, nil, nil, nil, nil, nil) if err != nil { return fmt.Errorf(errorMessage, segmentID, err) @@ -1343,7 +1349,11 @@ func nsxtPolicySegmentRead(d *schema.ResourceData, m interface{}, isVlan bool, i gwPath = d.Get("connectivity_path").(string) } - obj, err := nsxtPolicyGetSegment(getSessionContext(d, m), connector, id, gwPath, isFixed) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := nsxtPolicyGetSegment(context, connector, id, gwPath, isFixed) if err != nil { return handleReadError(d, "Segment", id, err) @@ -1451,17 +1461,21 @@ func nsxtPolicySegmentCreate(d *schema.ResourceData, m interface{}, isVlan bool, gwPath = d.Get("connectivity_path").(string) } - id, err := getOrGenerateID2(d, m, resourceNsxtPolicySegmentExists(getSessionContext(d, m), gwPath, isFixed)) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + id, err := getOrGenerateID2(d, m, resourceNsxtPolicySegmentExists(context, gwPath, isFixed)) if err != nil { return err } - obj, err := policySegmentResourceToInfraStruct(getSessionContext(d, m), id, d, isVlan, isFixed) + obj, err := policySegmentResourceToInfraStruct(context, id, d, isVlan, isFixed) if err != nil { return err } - err = policyInfraPatch(getSessionContext(d, m), obj, getPolicyConnector(m), false) + err = policyInfraPatch(context, obj, getPolicyConnector(m), false) if err != nil { return handleCreateError("Segment", id, err) } @@ -1479,12 +1493,16 @@ func nsxtPolicySegmentUpdate(d *schema.ResourceData, m interface{}, isVlan bool, return fmt.Errorf("Error obtaining Segment ID") } - obj, err := policySegmentResourceToInfraStruct(getSessionContext(d, m), id, d, isVlan, isFixed) + context, err := getSessionContext(d, m) + if err != nil { + return err + } + obj, err := policySegmentResourceToInfraStruct(context, id, d, isVlan, isFixed) if err != nil { return err } - err = policyInfraPatch(getSessionContext(d, m), obj, getPolicyConnector(m), true) + err = policyInfraPatch(context, obj, getPolicyConnector(m), true) if err != nil { return handleCreateError("Segment", id, err) } @@ -1499,6 +1517,10 @@ func nsxtPolicySegmentDelete(d *schema.ResourceData, m interface{}, isFixed bool } connector := getPolicyConnector(m) + context, err := getSessionContext(d, m) + if err != nil { + return err + } // During bulk destroy, VMs might be destroyed before segments, but // VIF release is not yet propagated to NSX. NSX will reply with @@ -1521,7 +1543,7 @@ func nsxtPolicySegmentDelete(d *schema.ResourceData, m interface{}, isFixed bool numOfPorts = len(gmPorts.Results) ports = gmPorts } else { - portsClient := segments.NewPortsClient(getSessionContext(d, m), connector) + portsClient := segments.NewPortsClient(context, connector) lmPorts, err := portsClient.List(id, nil, nil, nil, nil, nil, nil) if err != nil { return lmPorts, "error", logAPIError("Error listing segment ports", err) @@ -1588,7 +1610,7 @@ func nsxtPolicySegmentDelete(d *schema.ResourceData, m interface{}, isFixed bool } log.Printf("[DEBUG] Using H-API to delete segment with ID %s", id) - err := policyInfraPatch(getSessionContext(d, m), infraObj, getPolicyConnector(m), false) + err = policyInfraPatch(context, infraObj, getPolicyConnector(m), false) if err != nil { return handleDeleteError("Segment", id, err) }