Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource policy attachment #12812

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions mmv1/products/compute/ResourcePolicyAttachment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2024 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.

---
name: 'ResourcePolicyAttachment'
api_resource_type_kind: Instance
description: 'Attaches resource policies to compute instances'
id_format: '{{project}}/{{zone}}/{{instance}}/{{name}}'
base_url: 'projects/{{project}}/zones/{{zone}}/instances/{{instance}}'
self_link: 'projects/{{project}}/zones/{{zone}}/instances/{{instance}}'
create_url: 'projects/{{project}}/zones/{{zone}}/instances/{{instance}}/addResourcePolicies'
delete_url: 'projects/{{project}}/zones/{{zone}}/instances/{{instance}}/removeResourcePolicies'
delete_verb: 'POST'
immutable: true

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
timeouts:
insert_minutes: 20
delete_minutes: 20

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
async:
actions: ['create', 'delete']
type: 'OpAsync'
operation:
base_url: '{{op_id}}'
result:
resource_inside_response: false

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
identity:
- name

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
nested_query:
keys:
- resourcePolicies
is_list_of_ids: true
modify_by_patch: false

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
custom_code:
encoder: 'templates/terraform/encoders/compute_resource_policy_attachment.go.tmpl'
decoder: 'templates/terraform/decoders/compute_resource_policy_attachment.go.tmpl'
pre_delete: 'templates/terraform/pre_delete/compute_resource_policy_attachment.go.tmpl'

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
parameters:
- name: 'instance'
type: ResourceRef
required: true
resource: 'Instance'
imports: 'name'
- name: 'zone'
type: ResourceRef
required: true
resource: 'Zone'
imports: 'name'

Dawid212 marked this conversation as resolved.
Show resolved Hide resolved
properties:
- name: 'name'
type: String
required: true
immutable: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
res["name"] = tpgresource.GetResourceNameFromSelfLink(res["name"].(string))
return res, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
config := meta.(*transport_tpg.Config)
project, err := tpgresource.GetProject(d, config)
if err != nil {
return nil, err
}

zone, err := tpgresource.GetZone(d, config)
if err != nil {
return nil, err
}
if zone == "" {
return nil, fmt.Errorf("zone required")
}

region := tpgresource.GetRegionFromZone(zone)
if region == "" {
return nil, fmt.Errorf("invalid zone %q", zone)
}

obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])}
delete(obj, "name")
return obj, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
resource "google_compute_resource_policy" "policy" {
name = var.policy_name
region = "us-central1"

instance_schedule_policy {
vm_start_schedule {
schedule = "0 8 * * *"
}
vm_stop_schedule {
schedule = "0 20 * * *"
}
time_zone = "US/Central"
}
}

resource "google_compute_instance" "instance" {
name = var.instance_name
machine_type = "e2-micro"
zone = var.zone

boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}

network_interface {
network = "default"
}
}

resource "google_compute_resource_policy_attachment" "attachment" {
name = google_compute_resource_policy.policy.name
instance = google_compute_instance.instance.name
zone = var.zone
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
obj = make(map[string]interface{})

zone, err := tpgresource.GetZone(d, config)
if err != nil {
return err
}

region := tpgresource.GetRegionFromZone(zone)
if region == "" {
return fmt.Errorf("invalid zone %q", zone)
}

name, err := expandNestedComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config)
if err != nil {
return err
}

obj["resourcePolicies"] = []interface{}{
fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, name),
}
return nil
Loading