@@ -40,7 +40,8 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
4040 if hasExpectedBlocksAsAttributes (resourceb ) {
4141 return false , nil
4242 }
43- if err := convertRepSpecs (resourceb ); err != nil {
43+ diskSizeGB , _ := hcl .PopAttr (resourceb , nDiskSizeGB , errRoot ) // ok to fail as it's optional
44+ if err := convertRepSpecs (resourceb , diskSizeGB ); err != nil {
4445 return false , err
4546 }
4647 if err := fillTagsLabelsOpt (resourceb , nTags ); err != nil {
@@ -56,18 +57,29 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
5657 return true , nil
5758}
5859
59- func convertRepSpecs (resourceb * hclwrite.Body ) error {
60+ func convertRepSpecs (resourceb * hclwrite.Body , diskSizeGB hclwrite. Tokens ) error {
6061 var repSpecs []* hclwrite.Body
6162 for {
6263 block := resourceb .FirstMatchingBlock (nRepSpecs , nil )
6364 if block == nil {
6465 break
6566 }
6667 resourceb .RemoveBlock (block )
67- if err := convertConfig (block .Body ()); err != nil {
68+ blockb := block .Body ()
69+ numShardsVal := 1 // default to 1 if num_shards not present
70+ if numShardsAttr := blockb .GetAttribute (nNumShards ); numShardsAttr != nil {
71+ var err error
72+ if numShardsVal , err = hcl .GetAttrInt (numShardsAttr , errNumShards ); err != nil {
73+ return err
74+ }
75+ blockb .RemoveAttribute (nNumShards )
76+ }
77+ if err := convertConfig (blockb , diskSizeGB ); err != nil {
6878 return err
6979 }
70- repSpecs = append (repSpecs , block .Body ())
80+ for range numShardsVal {
81+ repSpecs = append (repSpecs , blockb )
82+ }
7183 }
7284 if len (repSpecs ) == 0 {
7385 return fmt .Errorf ("must have at least one replication_specs" )
@@ -76,7 +88,7 @@ func convertRepSpecs(resourceb *hclwrite.Body) error {
7688 return nil
7789}
7890
79- func convertConfig (repSpecs * hclwrite.Body ) error {
91+ func convertConfig (repSpecs * hclwrite.Body , diskSizeGB hclwrite. Tokens ) error {
8092 var configs []* hclwrite.Body
8193 for {
8294 block := repSpecs .FirstMatchingBlock (nConfig , nil )
@@ -85,11 +97,11 @@ func convertConfig(repSpecs *hclwrite.Body) error {
8597 }
8698 repSpecs .RemoveBlock (block )
8799 blockb := block .Body ()
88- fillBlockOpt (blockb , nElectableSpecs )
89- fillBlockOpt (blockb , nReadOnlySpecs )
90- fillBlockOpt (blockb , nAnalyticsSpecs )
91- fillBlockOpt (blockb , nAutoScaling )
92- fillBlockOpt (blockb , nAnalyticsAutoScaling )
100+ fillSpecOpt (blockb , nElectableSpecs , diskSizeGB )
101+ fillSpecOpt (blockb , nReadOnlySpecs , diskSizeGB )
102+ fillSpecOpt (blockb , nAnalyticsSpecs , diskSizeGB )
103+ fillSpecOpt (blockb , nAutoScaling , nil ) // auto_scaling doesn't need disk_size_gb
104+ fillSpecOpt (blockb , nAnalyticsAutoScaling , nil ) // analytics_auto_scaling doesn't need disk_size_gb
93105 configs = append (configs , blockb )
94106 }
95107 if len (configs ) == 0 {
@@ -99,6 +111,19 @@ func convertConfig(repSpecs *hclwrite.Body) error {
99111 return nil
100112}
101113
114+ func fillSpecOpt (resourceb * hclwrite.Body , name string , diskSizeGBTokens hclwrite.Tokens ) {
115+ block := resourceb .FirstMatchingBlock (name , nil )
116+ if block == nil {
117+ return
118+ }
119+ if diskSizeGBTokens != nil {
120+ blockb := block .Body ()
121+ blockb .RemoveAttribute (nDiskSizeGB )
122+ blockb .SetAttributeRaw (nDiskSizeGB , diskSizeGBTokens )
123+ }
124+ fillBlockOpt (resourceb , name )
125+ }
126+
102127// hasExpectedBlocksAsAttributes checks if any of the expected block names
103128// exist as attributes in the resource body. In that case conversion is not done
104129// as advanced cluster is not in a valid SDKv2 configuration.
0 commit comments