@@ -170,40 +170,49 @@ func loadBlock(d *schema.ResourceData, reservedBlock *packngo.IPAddressReservati
170170 ipv4CIDRToQuantity := map [int ]int {32 : 1 , 31 : 2 , 30 : 4 , 29 : 8 , 28 : 16 , 27 : 32 , 26 : 64 , 25 : 128 , 24 : 256 }
171171
172172 d .SetId (reservedBlock .ID )
173- d .Set ("address" , reservedBlock .Address )
174- if reservedBlock .Facility != nil {
175- d .Set ("facility" , reservedBlock .Facility .Code )
176- }
177- d .Set ("gateway" , reservedBlock .Gateway )
178- d .Set ("network" , reservedBlock .Network )
179- d .Set ("netmask" , reservedBlock .Netmask )
180- d .Set ("address_family" , reservedBlock .AddressFamily )
181- d .Set ("cidr" , reservedBlock .CIDR )
173+
182174 typ , err := getType (reservedBlock )
183175 if err != nil {
184176 return err
185177 }
186- d .Set ("type" , typ )
187- d .Set ("public" , reservedBlock .Public )
188- d .Set ("management" , reservedBlock .Management )
189- d .Set ("manageable" , reservedBlock .Manageable )
178+ quantity := 0
190179 if reservedBlock .AddressFamily == 4 {
191- d . Set ( " quantity" , ipv4CIDRToQuantity [reservedBlock .CIDR ])
180+ quantity = ipv4CIDRToQuantity [reservedBlock .CIDR ]
192181 } else {
193- // In Equinix Metal, a reserved IPv6 block is allocated when a device is run in a project.
194- // It's always /56, and it can't be created with Terraform, only imported.
195- // The longest assignable prefix is /64, making it max 256 subnets per block.
196- // The following logic will hold as long as /64 is the smallest assignable subnet size.
182+ // In Equinix Metal, a reserved IPv6 block is allocated when a device is
183+ // run in a project. It's always /56, and it can't be created with
184+ // Terraform, only imported. The longest assignable prefix is /64,
185+ // making it max 256 subnets per block. The following logic will hold as
186+ // long as /64 is the smallest assignable subnet size.
197187 bits := 64 - reservedBlock .CIDR
198188 if bits > 30 {
199189 return fmt .Errorf ("Strange (too small) CIDR prefix: %d" , reservedBlock .CIDR )
200190 }
201- d . Set ( " quantity" , 1 << uint (bits ) )
191+ quantity = 1 << uint (bits )
202192 }
203- d .Set ("project_id" , path .Base (reservedBlock .Project .Href ))
204- d .Set ("cidr_notation" , fmt .Sprintf ("%s/%d" , reservedBlock .Network , reservedBlock .CIDR ))
205- return nil
206193
194+ err = setMap (d , map [string ]interface {}{
195+ "address" : reservedBlock .Address ,
196+ "facility" : func (d * schema.ResourceData , k string ) error {
197+ if reservedBlock .Facility == nil {
198+ return nil
199+ }
200+ return d .Set (k , reservedBlock .Facility .Code )
201+ },
202+ "gateway" : reservedBlock .Gateway ,
203+ "network" : reservedBlock .Network ,
204+ "netmask" : reservedBlock .Netmask ,
205+ "address_family" : reservedBlock .AddressFamily ,
206+ "cidr" : reservedBlock .CIDR ,
207+ "type" : typ ,
208+ "public" : reservedBlock .Public ,
209+ "management" : reservedBlock .Management ,
210+ "manageable" : reservedBlock .Manageable ,
211+ "quantity" : quantity ,
212+ "project_id" : path .Base (reservedBlock .Project .Href ),
213+ "cidr_notation" : fmt .Sprintf ("%s/%d" , reservedBlock .Network , reservedBlock .CIDR ),
214+ })
215+ return err
207216}
208217
209218func resourcePacketReservedIPBlockRead (d * schema.ResourceData , meta interface {}) error {
@@ -221,13 +230,14 @@ func resourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{})
221230 return fmt .Errorf ("Error reading IP address block with ID %s: %s" , id , err )
222231 }
223232 err = loadBlock (d , reservedBlock )
233+ if err != nil {
234+ return err
235+ }
236+
224237 if (reservedBlock .Description != nil ) && (* (reservedBlock .Description ) != "" ) {
225238 d .Set ("description" , * (reservedBlock .Description ))
226239 }
227240 d .Set ("global" , getGlobalBool (reservedBlock ))
228- if err != nil {
229- return err
230- }
231241
232242 return nil
233243}
0 commit comments