Skip to content

Commit 8027d29

Browse files
Add HostProjectRegistration resource in API hub (#13092) (#9419)
[upstream:36067e81c1bfa85409f4a73e919fe1936d30e330] Signed-off-by: Modular Magician <[email protected]>
1 parent f3e48ac commit 8027d29

6 files changed

+522
-2
lines changed

.changelog/13092.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
`google_apihub_host_project_registration`
3+
```

google-beta/provider/provider_mmv1_resources.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{
538538
}
539539

540540
// Resources
541-
// Generated resources: 613
541+
// Generated resources: 614
542542
// Generated IAM resources: 309
543-
// Total generated resources: 922
543+
// Total generated resources: 923
544544
var generatedResources = map[string]*schema.Resource{
545545
"google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(),
546546
"google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(),
@@ -606,6 +606,7 @@ var generatedResources = map[string]*schema.Resource{
606606
"google_apigee_sync_authorization": apigee.ResourceApigeeSyncAuthorization(),
607607
"google_apigee_target_server": apigee.ResourceApigeeTargetServer(),
608608
"google_apihub_api_hub_instance": apihub.ResourceApihubApiHubInstance(),
609+
"google_apihub_host_project_registration": apihub.ResourceApihubHostProjectRegistration(),
609610
"google_app_engine_application_url_dispatch_rules": appengine.ResourceAppEngineApplicationUrlDispatchRules(),
610611
"google_app_engine_domain_mapping": appengine.ResourceAppEngineDomainMapping(),
611612
"google_app_engine_firewall_rule": appengine.ResourceAppEngineFirewallRule(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This code is generated by Magic Modules using the following:
11+
//
12+
// Configuration: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/products/apihub/HostProjectRegistration.yaml
13+
// Template: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/templates/terraform/resource.go.tmpl
14+
//
15+
// DO NOT EDIT this file directly. Any changes made to this file will be
16+
// overwritten during the next generation cycle.
17+
//
18+
// ----------------------------------------------------------------------------
19+
20+
package apihub
21+
22+
import (
23+
"fmt"
24+
"log"
25+
"net/http"
26+
"reflect"
27+
"time"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
31+
32+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
33+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
34+
)
35+
36+
func ResourceApihubHostProjectRegistration() *schema.Resource {
37+
return &schema.Resource{
38+
Create: resourceApihubHostProjectRegistrationCreate,
39+
Read: resourceApihubHostProjectRegistrationRead,
40+
Delete: resourceApihubHostProjectRegistrationDelete,
41+
42+
Importer: &schema.ResourceImporter{
43+
State: resourceApihubHostProjectRegistrationImport,
44+
},
45+
46+
Timeouts: &schema.ResourceTimeout{
47+
Create: schema.DefaultTimeout(20 * time.Minute),
48+
Delete: schema.DefaultTimeout(20 * time.Minute),
49+
},
50+
51+
CustomizeDiff: customdiff.All(
52+
tpgresource.DefaultProviderProject,
53+
),
54+
55+
Schema: map[string]*schema.Schema{
56+
"gcp_project": {
57+
Type: schema.TypeString,
58+
Required: true,
59+
ForceNew: true,
60+
DiffSuppressFunc: tpgresource.ProjectNumberDiffSuppress,
61+
Description: `Required. Immutable. Google cloud project name in the format: "projects/abc" or "projects/123".
62+
As input, project name with either project id or number are accepted.
63+
As output, this field will contain project number.`,
64+
},
65+
"host_project_registration_id": {
66+
Type: schema.TypeString,
67+
Required: true,
68+
ForceNew: true,
69+
Description: `Required. The ID to use for the Host Project Registration, which will become the
70+
final component of the host project registration's resource name. The ID
71+
must be the same as the Google cloud project specified in the
72+
host_project_registration.gcp_project field.`,
73+
},
74+
"location": {
75+
Type: schema.TypeString,
76+
Required: true,
77+
ForceNew: true,
78+
Description: `Part of 'parent'. See documentation of 'projectsId'.`,
79+
},
80+
"create_time": {
81+
Type: schema.TypeString,
82+
Computed: true,
83+
Description: `Output only. The time at which the host project registration was created.`,
84+
},
85+
"name": {
86+
Type: schema.TypeString,
87+
Computed: true,
88+
Description: `Identifier. The name of the host project registration.
89+
Format:
90+
"projects/{project}/locations/{location}/hostProjectRegistrations/{host_project_registration}".`,
91+
},
92+
"project": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
Computed: true,
96+
ForceNew: true,
97+
},
98+
},
99+
UseJSONNumber: true,
100+
}
101+
}
102+
103+
func resourceApihubHostProjectRegistrationCreate(d *schema.ResourceData, meta interface{}) error {
104+
config := meta.(*transport_tpg.Config)
105+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
106+
if err != nil {
107+
return err
108+
}
109+
110+
obj := make(map[string]interface{})
111+
gcpProjectProp, err := expandApihubHostProjectRegistrationGcpProject(d.Get("gcp_project"), d, config)
112+
if err != nil {
113+
return err
114+
} else if v, ok := d.GetOkExists("gcp_project"); !tpgresource.IsEmptyValue(reflect.ValueOf(gcpProjectProp)) && (ok || !reflect.DeepEqual(v, gcpProjectProp)) {
115+
obj["gcpProject"] = gcpProjectProp
116+
}
117+
118+
url, err := tpgresource.ReplaceVars(d, config, "{{ApihubBasePath}}projects/{{project}}/locations/{{location}}/hostProjectRegistrations?hostProjectRegistrationId={{host_project_registration_id}}")
119+
if err != nil {
120+
return err
121+
}
122+
123+
log.Printf("[DEBUG] Creating new HostProjectRegistration: %#v", obj)
124+
billingProject := ""
125+
126+
project, err := tpgresource.GetProject(d, config)
127+
if err != nil {
128+
return fmt.Errorf("Error fetching project for HostProjectRegistration: %s", err)
129+
}
130+
billingProject = project
131+
132+
// err == nil indicates that the billing_project value was found
133+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
134+
billingProject = bp
135+
}
136+
137+
headers := make(http.Header)
138+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
139+
Config: config,
140+
Method: "POST",
141+
Project: billingProject,
142+
RawURL: url,
143+
UserAgent: userAgent,
144+
Body: obj,
145+
Timeout: d.Timeout(schema.TimeoutCreate),
146+
Headers: headers,
147+
})
148+
if err != nil {
149+
return fmt.Errorf("Error creating HostProjectRegistration: %s", err)
150+
}
151+
if err := d.Set("name", flattenApihubHostProjectRegistrationName(res["name"], d, config)); err != nil {
152+
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
153+
}
154+
155+
// Store the ID now
156+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}")
157+
if err != nil {
158+
return fmt.Errorf("Error constructing id: %s", err)
159+
}
160+
d.SetId(id)
161+
162+
log.Printf("[DEBUG] Finished creating HostProjectRegistration %q: %#v", d.Id(), res)
163+
164+
return resourceApihubHostProjectRegistrationRead(d, meta)
165+
}
166+
167+
func resourceApihubHostProjectRegistrationRead(d *schema.ResourceData, meta interface{}) error {
168+
config := meta.(*transport_tpg.Config)
169+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
170+
if err != nil {
171+
return err
172+
}
173+
174+
url, err := tpgresource.ReplaceVars(d, config, "{{ApihubBasePath}}projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}")
175+
if err != nil {
176+
return err
177+
}
178+
179+
billingProject := ""
180+
181+
project, err := tpgresource.GetProject(d, config)
182+
if err != nil {
183+
return fmt.Errorf("Error fetching project for HostProjectRegistration: %s", err)
184+
}
185+
billingProject = project
186+
187+
// err == nil indicates that the billing_project value was found
188+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
189+
billingProject = bp
190+
}
191+
192+
headers := make(http.Header)
193+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
194+
Config: config,
195+
Method: "GET",
196+
Project: billingProject,
197+
RawURL: url,
198+
UserAgent: userAgent,
199+
Headers: headers,
200+
})
201+
if err != nil {
202+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApihubHostProjectRegistration %q", d.Id()))
203+
}
204+
205+
if err := d.Set("project", project); err != nil {
206+
return fmt.Errorf("Error reading HostProjectRegistration: %s", err)
207+
}
208+
209+
if err := d.Set("name", flattenApihubHostProjectRegistrationName(res["name"], d, config)); err != nil {
210+
return fmt.Errorf("Error reading HostProjectRegistration: %s", err)
211+
}
212+
if err := d.Set("gcp_project", flattenApihubHostProjectRegistrationGcpProject(res["gcpProject"], d, config)); err != nil {
213+
return fmt.Errorf("Error reading HostProjectRegistration: %s", err)
214+
}
215+
if err := d.Set("create_time", flattenApihubHostProjectRegistrationCreateTime(res["createTime"], d, config)); err != nil {
216+
return fmt.Errorf("Error reading HostProjectRegistration: %s", err)
217+
}
218+
219+
return nil
220+
}
221+
222+
func resourceApihubHostProjectRegistrationDelete(d *schema.ResourceData, meta interface{}) error {
223+
log.Printf("[WARNING] Apihub HostProjectRegistration resources"+
224+
" cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+
225+
" state, but will still be present on Google Cloud.", d.Id())
226+
d.SetId("")
227+
228+
return nil
229+
}
230+
231+
func resourceApihubHostProjectRegistrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
232+
config := meta.(*transport_tpg.Config)
233+
if err := tpgresource.ParseImportId([]string{
234+
"^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/hostProjectRegistrations/(?P<host_project_registration_id>[^/]+)$",
235+
"^(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<host_project_registration_id>[^/]+)$",
236+
"^(?P<location>[^/]+)/(?P<host_project_registration_id>[^/]+)$",
237+
}, d, config); err != nil {
238+
return nil, err
239+
}
240+
241+
// Replace import id for the resource id
242+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}")
243+
if err != nil {
244+
return nil, fmt.Errorf("Error constructing id: %s", err)
245+
}
246+
d.SetId(id)
247+
248+
return []*schema.ResourceData{d}, nil
249+
}
250+
251+
func flattenApihubHostProjectRegistrationName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
252+
return v
253+
}
254+
255+
func flattenApihubHostProjectRegistrationGcpProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
256+
return v
257+
}
258+
259+
func flattenApihubHostProjectRegistrationCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
260+
return v
261+
}
262+
263+
func expandApihubHostProjectRegistrationGcpProject(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
264+
return v, nil
265+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource: 'google_apihub_host_project_registration'
2+
generation_type: 'mmv1'
3+
source_file: 'products/apihub/HostProjectRegistration.yaml'
4+
api_service_name: 'apihub.googleapis.com'
5+
api_version: 'v1'
6+
api_resource_type_kind: 'HostProjectRegistration'
7+
autogen_status: true
8+
fields:
9+
- field: 'create_time'
10+
- field: 'gcp_project'
11+
- field: 'host_project_registration_id'
12+
provider_only: true
13+
- field: 'location'
14+
provider_only: true
15+
- field: 'name'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This file is automatically generated by Magic Modules and manual
11+
// changes will be clobbered when the file is regenerated.
12+
//
13+
// Please read more about how to change this file in
14+
// .github/CONTRIBUTING.md.
15+
//
16+
// ----------------------------------------------------------------------------
17+
18+
package apihub_test
19+
20+
import (
21+
"testing"
22+
23+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
24+
25+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
26+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
27+
)
28+
29+
func TestAccApihubHostProjectRegistration_apihubHostProjectRegistrationBasicExample(t *testing.T) {
30+
t.Parallel()
31+
32+
context := map[string]interface{}{
33+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
34+
"org_id": envvar.GetTestOrgFromEnv(t),
35+
"random_suffix": acctest.RandString(t, 10),
36+
}
37+
38+
acctest.VcrTest(t, resource.TestCase{
39+
PreCheck: func() { acctest.AccTestPreCheck(t) },
40+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
41+
ExternalProviders: map[string]resource.ExternalProvider{
42+
"time": {},
43+
},
44+
Steps: []resource.TestStep{
45+
{
46+
Config: testAccApihubHostProjectRegistration_apihubHostProjectRegistrationBasicExample(context),
47+
},
48+
{
49+
ResourceName: "google_apihub_host_project_registration.apihub_host_project",
50+
ImportState: true,
51+
ImportStateVerify: true,
52+
ImportStateVerifyIgnore: []string{"host_project_registration_id", "location"},
53+
},
54+
},
55+
})
56+
}
57+
58+
func testAccApihubHostProjectRegistration_apihubHostProjectRegistrationBasicExample(context map[string]interface{}) string {
59+
return acctest.Nprintf(`
60+
resource "google_project" "project" {
61+
name = "tf-test-apihub-proj%{random_suffix}"
62+
project_id = "tf-test-apihub-proj%{random_suffix}"
63+
org_id = "%{org_id}"
64+
billing_account = "%{billing_account}"
65+
deletion_policy = "DELETE"
66+
}
67+
68+
resource "time_sleep" "wait_60_seconds" {
69+
create_duration = "60s"
70+
depends_on = [google_project.project]
71+
}
72+
73+
# Enable API hub API
74+
resource "google_project_service" "apihub_service" {
75+
project = google_project.project.project_id
76+
service = "apihub.googleapis.com"
77+
depends_on = [time_sleep.wait_60_seconds]
78+
}
79+
80+
resource "google_apihub_host_project_registration" "apihub_host_project"{
81+
project = google_project.project.project_id
82+
location = "asia-south1"
83+
host_project_registration_id = google_project.project.project_id
84+
gcp_project = "projects/${google_project.project.project_id}"
85+
86+
depends_on = [google_project_service.apihub_service]
87+
}
88+
`, context)
89+
}

0 commit comments

Comments
 (0)