55package nsxt
66
77import (
8+ "context"
89 "fmt"
910 "log"
1011
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214 "github.com/vmware/terraform-provider-nsxt/nsxt/util"
1315 "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
1416 infra2 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
@@ -27,6 +29,19 @@ func resourceNsxtPolicyProject() *schema.Resource {
2729 State : schema .ImportStatePassthrough ,
2830 },
2931
32+ CustomizeDiff : func (ctx context.Context , d * schema.ResourceDiff , m interface {}) error {
33+ if util .NsxVersionHigherOrEqual ("9.1.0" ) {
34+ c := m .(nsxtClients )
35+ raw := d .GetRawConfig ()
36+ newAttr := raw .GetAttr ("default_span_path" )
37+
38+ if newAttr .IsNull () {
39+ d .SetNew ("default_span_path" , c .DefaultSpanPath )
40+ return nil
41+ }
42+ }
43+ return nil
44+ },
3045 Schema : map [string ]* schema.Schema {
3146 "nsx_id" : getNsxIDSchema (),
3247 "path" : getPathSchema (),
@@ -136,6 +151,12 @@ func resourceNsxtPolicyProject() *schema.Resource {
136151 Type : schema .TypeString ,
137152 },
138153 },
154+ "id_suffix" : {
155+ Type : schema .TypeString ,
156+ Description : "Suffix to be appended to the IDs of the project's default objects." ,
157+ Optional : true ,
158+ ValidateFunc : validation .StringLenBetween (0 , 246 ),
159+ },
139160 },
140161 }
141162}
@@ -211,22 +232,17 @@ func resourceNsxtPolicyProjectPatch(connector client.Connector, d *schema.Resour
211232 }
212233
213234 if util .NsxVersionHigherOrEqual ("9.1.0" ) {
214- // There should be just one object here
215235 var spanReferences []model.SpanReference
216- defaultSpanPathinterface , isDefaultSet := d .GetOkExists ("default_span_path" )
236+
237+ isDefault := true
238+ defaultSpanPathInterface , isDefaultSet := d .GetOkExists ("default_span_path" )
217239 var defaultSpanPath string
218- if ! isDefaultSet {
219- var err error
220- defaultSpanPath , err = getDefaultSpan (connector )
221- if err != nil {
222- return err
223- }
240+ if isDefaultSet {
241+ defaultSpanPath = defaultSpanPathInterface .(string )
224242 } else {
225- defaultSpanPath = defaultSpanPathinterface .( string )
243+ defaultSpanPath = m .( nsxtClients ). DefaultSpanPath
226244 }
227- // default_span_path will never be empty, since it has a default value and the validator will make sure that
228- // user will not assign an empty string or such.
229- isDefault := true
245+
230246 spanReferences = append (spanReferences , model.SpanReference {
231247 SpanPath : & defaultSpanPath ,
232248 IsDefault : & isDefault ,
@@ -248,6 +264,11 @@ func resourceNsxtPolicyProjectPatch(connector client.Connector, d *schema.Resour
248264 ZoneExternalIds : zoneExternalIds ,
249265 }
250266 obj .VpcDeploymentScope = & vpcDeploymentScope
267+
268+ // Set id_suffix if provided
269+ if idSuffix := d .Get ("id_suffix" ).(string ); idSuffix != "" {
270+ obj .IdSuffix = & idSuffix
271+ }
251272 }
252273
253274 log .Printf ("[INFO] Patching Project with ID %s" , id )
@@ -410,20 +431,24 @@ func resourceNsxtPolicyProjectRead(d *schema.ResourceData, m interface{}) error
410431 d .Set ("quotas" , obj .Limits )
411432 }
412433
413- if util .NsxVersionHigherOrEqual ("9.1.0" ) && obj .VpcDeploymentScope != nil {
414- var nonDefaultSpanPaths []interface {}
415- var defaultSpanRefs * string
416- for _ , spanRef := range obj .VpcDeploymentScope .SpanReferences {
417- if * spanRef .IsDefault {
418- defaultSpanRefs = spanRef .SpanPath
419- } else {
420- nonDefaultSpanPaths = append (nonDefaultSpanPaths , * spanRef .SpanPath )
434+ if util .NsxVersionHigherOrEqual ("9.1.0" ) {
435+ // Set id_suffix if available
436+ d .Set ("id_suffix" , obj .IdSuffix )
437+
438+ if obj .VpcDeploymentScope != nil {
439+ var nonDefaultSpanPaths []interface {}
440+ var defaultSpanRefs * string
441+ for _ , spanRef := range obj .VpcDeploymentScope .SpanReferences {
442+ if * spanRef .IsDefault {
443+ defaultSpanRefs = spanRef .SpanPath
444+ } else {
445+ nonDefaultSpanPaths = append (nonDefaultSpanPaths , * spanRef .SpanPath )
446+ }
421447 }
448+ d .Set ("default_span_path" , defaultSpanRefs )
449+ d .Set ("non_default_span_paths" , nonDefaultSpanPaths )
450+ d .Set ("zone_external_ids" , stringList2Interface (obj .VpcDeploymentScope .ZoneExternalIds ))
422451 }
423- d .Set ("default_span_path" , defaultSpanRefs )
424- d .Set ("non_default_span_paths" , nonDefaultSpanPaths )
425- d .Set ("zone_external_ids" , stringList2Interface (obj .VpcDeploymentScope .ZoneExternalIds ))
426-
427452 }
428453 return nil
429454}
0 commit comments