1818package compute
1919
2020import (
21+ "bytes"
2122 "fmt"
2223 "log"
2324 "reflect"
@@ -30,6 +31,42 @@ import (
3031 transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3132)
3233
34+ // Hash based on key, which is either project_id_or_num or network_url.
35+ func computeServiceAttachmentConsumerAcceptListsHash (v interface {}) int {
36+ if v == nil {
37+ return 0
38+ }
39+
40+ var buf bytes.Buffer
41+ m := v .(map [string ]interface {})
42+ log .Printf ("[DEBUG] hashing %v" , m )
43+
44+ if v , ok := m ["project_id_or_num" ]; ok {
45+ if v == nil {
46+ v = ""
47+ }
48+
49+ buf .WriteString (fmt .Sprintf ("%v-" , v ))
50+ }
51+
52+ if v , ok := m ["network_url" ]; ok {
53+ if v == nil {
54+ v = ""
55+ } else {
56+ if networkUrl , err := tpgresource .GetRelativePath (v .(string )); err != nil {
57+ log .Printf ("[WARN] Error on retrieving relative path of network url: %s" , err )
58+ } else {
59+ v = networkUrl
60+ }
61+ }
62+
63+ buf .WriteString (fmt .Sprintf ("%v-" , v ))
64+ }
65+
66+ log .Printf ("[DEBUG] computed hash value of %v from %v" , tpgresource .Hashcode (buf .String ()), buf .String ())
67+ return tpgresource .Hashcode (buf .String ())
68+ }
69+
3370func ResourceComputeServiceAttachment () * schema.Resource {
3471 return & schema.Resource {
3572 Create : resourceComputeServiceAttachmentCreate ,
@@ -100,7 +137,7 @@ this service attachment.`,
100137 Description : `An array of projects that are allowed to connect to this service
101138attachment.` ,
102139 Elem : computeServiceAttachmentConsumerAcceptListsSchema (),
103- // Default schema.HashSchema is used.
140+ Set : computeServiceAttachmentConsumerAcceptListsHash ,
104141 },
105142 "consumer_reject_lists" : {
106143 Type : schema .TypeList ,
@@ -195,11 +232,19 @@ func computeServiceAttachmentConsumerAcceptListsSchema() *schema.Resource {
195232 Required : true ,
196233 Description : `The number of consumer forwarding rules the consumer project can
197234create.` ,
235+ },
236+ "network_url" : {
237+ Type : schema .TypeString ,
238+ Optional : true ,
239+ DiffSuppressFunc : tpgresource .CompareSelfLinkRelativePaths ,
240+ Description : `The network that is allowed to connect to this service attachment.
241+ Only one of project_id_or_num and network_url may be set.` ,
198242 },
199243 "project_id_or_num" : {
200- Type : schema .TypeString ,
201- Required : true ,
202- Description : `A project that is allowed to connect to this service attachment.` ,
244+ Type : schema .TypeString ,
245+ Optional : true ,
246+ Description : `A project that is allowed to connect to this service attachment.
247+ Only one of project_id_or_num and network_url may be set.` ,
203248 },
204249 },
205250 }
@@ -688,7 +733,7 @@ func flattenComputeServiceAttachmentConsumerAcceptLists(v interface{}, d *schema
688733 return v
689734 }
690735 l := v .([]interface {})
691- transformed := schema .NewSet (schema . HashResource ( computeServiceAttachmentConsumerAcceptListsSchema ()) , []interface {}{})
736+ transformed := schema .NewSet (computeServiceAttachmentConsumerAcceptListsHash , []interface {}{})
692737 for _ , raw := range l {
693738 original := raw .(map [string ]interface {})
694739 if len (original ) < 1 {
@@ -697,6 +742,7 @@ func flattenComputeServiceAttachmentConsumerAcceptLists(v interface{}, d *schema
697742 }
698743 transformed .Add (map [string ]interface {}{
699744 "project_id_or_num" : flattenComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum (original ["projectIdOrNum" ], d , config ),
745+ "network_url" : flattenComputeServiceAttachmentConsumerAcceptListsNetworkUrl (original ["networkUrl" ], d , config ),
700746 "connection_limit" : flattenComputeServiceAttachmentConsumerAcceptListsConnectionLimit (original ["connectionLimit" ], d , config ),
701747 })
702748 }
@@ -706,6 +752,10 @@ func flattenComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum(v interfac
706752 return v
707753}
708754
755+ func flattenComputeServiceAttachmentConsumerAcceptListsNetworkUrl (v interface {}, d * schema.ResourceData , config * transport_tpg.Config ) interface {} {
756+ return v
757+ }
758+
709759func flattenComputeServiceAttachmentConsumerAcceptListsConnectionLimit (v interface {}, d * schema.ResourceData , config * transport_tpg.Config ) interface {} {
710760 // Handles the string fixed64 format
711761 if strVal , ok := v .(string ); ok {
@@ -797,6 +847,13 @@ func expandComputeServiceAttachmentConsumerAcceptLists(v interface{}, d tpgresou
797847 transformed ["projectIdOrNum" ] = transformedProjectIdOrNum
798848 }
799849
850+ transformedNetworkUrl , err := expandComputeServiceAttachmentConsumerAcceptListsNetworkUrl (original ["network_url" ], d , config )
851+ if err != nil {
852+ return nil , err
853+ } else if val := reflect .ValueOf (transformedNetworkUrl ); val .IsValid () && ! tpgresource .IsEmptyValue (val ) {
854+ transformed ["networkUrl" ] = transformedNetworkUrl
855+ }
856+
800857 transformedConnectionLimit , err := expandComputeServiceAttachmentConsumerAcceptListsConnectionLimit (original ["connection_limit" ], d , config )
801858 if err != nil {
802859 return nil , err
@@ -813,6 +870,10 @@ func expandComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum(v interface
813870 return v , nil
814871}
815872
873+ func expandComputeServiceAttachmentConsumerAcceptListsNetworkUrl (v interface {}, d tpgresource.TerraformResourceData , config * transport_tpg.Config ) (interface {}, error ) {
874+ return v , nil
875+ }
876+
816877func expandComputeServiceAttachmentConsumerAcceptListsConnectionLimit (v interface {}, d tpgresource.TerraformResourceData , config * transport_tpg.Config ) (interface {}, error ) {
817878 return v , nil
818879}
0 commit comments