Skip to content

Commit

Permalink
custom_flatten changes
Browse files Browse the repository at this point in the history
  • Loading branch information
amanMahendroo committed Mar 27, 2024
1 parent 7e29210 commit b789ae1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions mmv1/products/compute/Router.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ properties:
ranges will be advertised in addition to any specified groups.
Leave this field blank to advertise no custom IP ranges.
send_empty_value: true
custom_flatten: 'templates/terraform/custom_flatten/reorder_ip_ranges.go.erb'
item_type: !ruby/object:Api::Type::NestedObject
properties:
- !ruby/object:Api::Type::String
Expand Down
65 changes: 65 additions & 0 deletions mmv1/templates/terraform/custom_flatten/reorder_ip_ranges.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<%# The license inside this block applies to this file.
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.([]interface{})
if len(original) == 0 {
return nil
}
transformed := make([]interface{})
source := d.Get("bgp.0.advertised_ip_ranges").([]interface{})
new_entries = make([]interface{})

for _, val1 := range original {
found := false
for _, val2 := range source {
if val1 == val2 {
found = true
break
}
}
if !found {
new_entries = append(new_entries, val1)
}
}

i := 0
for _, val1 := range source {
found := false
for _, val2 := range original {
if val1 == val2 {
found = true
break
}
}
if found {
transformed = append(transformed, val1)
} else {
if len(new_entries) > i {
transformed = append(transformed, new_entries[i])
i++
}
}
}

for i <= len(new_entries) {
transformed = append(transformed, new_entries[i])
i++
}

return transformed
}

0 comments on commit b789ae1

Please sign in to comment.