@@ -21,6 +21,7 @@ func TestAccComputePublicPrefixes(t *testing.T) {
2121 "public_delegated_prefixes_ipv6" : testAccComputePublicDelegatedPrefix_publicDelegatedPrefixesIpv6Test ,
2222 "public_advertised_prefixes_pdp_scope" : testAccComputePublicAdvertisedPrefix_publicAdvertisedPrefixesPdpScopeTest ,
2323 "public_delegated_prefix_ipv6_subnet_mode" : testAccComputePublicDelegatedPrefix_publicDelegatedPrefixIpv6SubnetModeTest ,
24+ "public_delgated_prefix_with_sub_prefix" : TestAccComputePublicDelegatedPrefix_computePublicDelegatedPrefixWithSubPrefixExample ,
2425 }
2526
2627 for name , tc := range testCases {
@@ -35,6 +36,84 @@ func TestAccComputePublicPrefixes(t *testing.T) {
3536 }
3637}
3738
39+ func TestAccComputePublicDelegatedPrefix_computePublicDelegatedPrefixWithSubPrefixExample (t * testing.T ) {
40+ t .Parallel ()
41+ subPrefixResourceName := "google_compute_public_delegated_prefix.subprefix"
42+ parentProject := "tf-static-byoip"
43+ parentRegion := "us-central1"
44+ parentName := "tf-test-delegation-mode-sub-pdp"
45+
46+ context := map [string ]interface {}{
47+ "parent_pdp_id" : "projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/tf-test-delegation-mode-sub-pdp" ,
48+ "project" : "tf-static-byoip" ,
49+ "random_suffix" : acctest .RandString (t , 10 ),
50+ }
51+
52+ acctest .VcrTest (t , resource.TestCase {
53+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
54+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
55+ CheckDestroy : testAccCheckComputePublicDelegatedPrefixDestroyProducer (t ),
56+ Steps : []resource.TestStep {
57+ {
58+ Config : testAccComputePublicDelegatedPrefix_computePublicDelegatedPrefixWithSubPrefixExample (context ),
59+ Check : resource .ComposeTestCheckFunc (
60+ // First, a basic check that the sub-prefix was created
61+ resource .TestCheckResourceAttrSet (subPrefixResourceName , "id" ),
62+
63+ // Now, the custom check function
64+ testAccCheckParentHasSubPrefix (t , parentProject , parentRegion , parentName , subPrefixResourceName ),
65+ ),
66+ },
67+ {
68+ ResourceName : "google_compute_public_delegated_prefix.subprefix" ,
69+ ImportState : true ,
70+ ImportStateVerify : true ,
71+ ImportStateVerifyIgnore : []string {"region" },
72+ },
73+ },
74+ })
75+ }
76+
77+ func testAccComputePublicDelegatedPrefix_computePublicDelegatedPrefixWithSubPrefixExample (context map [string ]interface {}) string {
78+ return acctest .Nprintf (`
79+
80+ resource "google_compute_public_delegated_prefix" "subprefix" {
81+ name = "tf-test-sub-prefix-1%{random_suffix}"
82+ description = "A nested address"
83+ region = "us-central1"
84+ ip_cidr_range = "2600:1901:4500:2::/64"
85+ parent_prefix = "%{parent_pdp_id}"
86+ mode = "DELEGATION"
87+ }
88+ ` , context )
89+ }
90+
91+ func testAccCheckParentHasSubPrefix (t * testing.T , project , region , parentName , subPrefixResourceName string ) resource.TestCheckFunc {
92+ return func (s * terraform.State ) error {
93+ rs , ok := s .RootModule ().Resources [subPrefixResourceName ]
94+ if ! ok {
95+ return fmt .Errorf ("Not found: %s" , subPrefixResourceName )
96+ }
97+ newSubPrefixName := rs .Primary .Attributes ["name" ]
98+
99+ config := acctest .GoogleProviderConfig (t )
100+ computeService := config .NewComputeClient (config .UserAgent )
101+
102+ parent , err := computeService .PublicDelegatedPrefixes .Get (project , region , parentName ).Do ()
103+ if err != nil {
104+ return err
105+ }
106+
107+ for _ , sub := range parent .PublicDelegatedSubPrefixs {
108+ if sub .Name == newSubPrefixName {
109+ return nil
110+ }
111+ }
112+
113+ return fmt .Errorf ("Sub-Prefix %q not found in parent %q's sub-prefix list" , newSubPrefixName , parentName )
114+ }
115+ }
116+
38117func testAccComputePublicAdvertisedPrefix_publicAdvertisedPrefixesPdpScopeTest (t * testing.T ) {
39118 context := map [string ]interface {}{
40119 "description" : envvar .GetTestPublicAdvertisedPrefixDescriptionFromEnv (t ),
0 commit comments