adding new resource to allow iam bindings on GCE instances

This commit is contained in:
Richard Hsu 2019-05-02 12:03:57 -04:00
parent 966229afbe
commit b907b5a0c0
4 changed files with 460 additions and 0 deletions

View File

@ -62,6 +62,10 @@ func ParseMachineTypesFieldValue(machineType string, d TerraformResourceData, co
return parseZonalFieldValue("machineTypes", machineType, "project", "zone", d, config, false)
}
func ParseInstanceFieldValue(instance string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
return parseZonalFieldValue("instances", instance, "project", "zone", d, config, false)
}
func ParseInstanceGroupFieldValue(instanceGroup string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
return parseZonalFieldValue("instanceGroups", instanceGroup, "project", "zone", d, config, false)
}

View File

@ -0,0 +1,147 @@
package google
import (
"fmt"
"strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/compute/v1"
)
var IamComputeInstanceSchema = map[string]*schema.Schema{
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
}
type ComputeInstanceIamUpdater struct {
project string
zone string
resourceId string
Config *Config
}
func NewComputeInstanceIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
zone, err := getZone(d, config)
if err != nil {
return nil, err
}
return &ComputeInstanceIamUpdater{
project: project,
zone: zone,
resourceId: d.Get("instance_name").(string),
Config: config,
}, nil
}
func ComputeInstanceIdParseFunc(d *schema.ResourceData, config *Config) error {
parts := strings.Split(d.Id(), "/")
var fv *ZonalFieldValue
if len(parts) == 3 {
// {project}/{zone}/{name} syntax
fv = &ZonalFieldValue{
Project: parts[0],
Zone: parts[1],
Name: parts[2],
resourceType: "instances",
}
} else if len(parts) == 2 {
// /{zone}/{name} syntax
project, err := getProject(d, config)
if err != nil {
return err
}
fv = &ZonalFieldValue{
Project: project,
Zone: parts[0],
Name: parts[1],
resourceType: "instances",
}
} else {
// We either have a name or a full self link, so use the field helper
var err error
fv, err = ParseInstanceFieldValue(d.Id(), d, config)
if err != nil {
return err
}
}
d.Set("project", fv.Project)
d.Set("zone", fv.Zone)
d.Set("instance_name", fv.Name)
// Explicitly set the id so imported resources have the same ID format as non-imported ones.
d.SetId(fv.RelativeLink())
return nil
}
func (u *ComputeInstanceIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
p, err := u.Config.clientCompute.Instances.GetIamPolicy(u.project, u.zone, u.resourceId).Do()
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
cloudResourcePolicy, err := computeToResourceManagerPolicy(p)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
return cloudResourcePolicy, nil
}
func (u *ComputeInstanceIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
computePolicy, err := resourceManagerToComputePolicy(policy)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
req := &compute.ZoneSetPolicyRequest{
Policy: computePolicy,
}
_, err = u.Config.clientCompute.Instances.SetIamPolicy(u.project, u.zone, u.resourceId, req).Do()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
return nil
}
func (u *ComputeInstanceIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/zones/%s/instances/%s", u.project, u.zone, u.resourceId)
}
func (u *ComputeInstanceIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-compute-Instance-%s-%s-%s", u.project, u.zone, u.resourceId)
}
func (u *ComputeInstanceIamUpdater) DescribeResource() string {
return fmt.Sprintf("Compute Instance %s/%s/%s", u.project, u.zone, u.resourceId)
}

View File

@ -173,6 +173,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_instance_from_template": resourceComputeInstanceFromTemplate(),
"google_compute_instance_group": resourceComputeInstanceGroup(),
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
"google_compute_instance_iam_binding": ResourceIamBindingWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc),
"google_compute_instance_iam_member": ResourceIamMemberWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc),
"google_compute_instance_iam_policy": ResourceIamPolicyWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc),
"google_compute_instance_template": resourceComputeInstanceTemplate(),
"google_compute_network_peering": resourceComputeNetworkPeering(),
"google_compute_project_metadata": resourceComputeProjectMetadata(),

View File

@ -0,0 +1,306 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccComputeInstanceIamBinding(t *testing.T) {
t.Parallel()
project := getTestProjectFromEnv()
account := acctest.RandomWithPrefix("tf-test")
role := "roles/compute.osLogin"
region := getTestRegionFromEnv()
zone := getTestZoneFromEnv()
subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10))
instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetwork, instanceName, role),
},
{
ResourceName: "google_compute_instance_iam_binding.foo",
ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role),
ImportState: true,
ImportStateVerify: true,
},
{
// Test Iam Binding update
Config: testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetwork, instanceName, role),
},
{
ResourceName: "google_compute_instance_iam_binding.foo",
ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccComputeInstanceIamMember(t *testing.T) {
t.Parallel()
project := getTestProjectFromEnv()
account := acctest.RandomWithPrefix("tf-test")
role := "roles/compute.osLogin"
region := getTestRegionFromEnv()
zone := getTestZoneFromEnv()
subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10))
instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
// Test Iam Member creation (no update for member, no need to test)
Config: testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetwork, instanceName, role),
},
{
ResourceName: "google_compute_instance_iam_member.foo",
ImportStateId: fmt.Sprintf("%s/%s/%s %s serviceAccount:%s@%s.iam.gserviceaccount.com", project, zone, instanceName, role, account, project),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccComputeInstanceIamPolicy(t *testing.T) {
t.Parallel()
project := getTestProjectFromEnv()
account := acctest.RandomWithPrefix("tf-test")
role := "roles/compute.osLogin"
region := getTestRegionFromEnv()
zone := getTestZoneFromEnv()
instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10))
subnetwork := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetwork, instanceName, role),
},
// Test a few import formats
{
ResourceName: "google_compute_instance_iam_policy.foo",
ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName),
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_compute_instance_iam_policy.foo",
ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName),
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_compute_instance_iam_policy.foo",
ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string {
return fmt.Sprintf(`
resource "google_service_account" "test_account" {
account_id = "%s"
display_name = "Iam Testing Account"
}
resource "google_compute_network" "network" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnetwork" {
name = "%s"
region = "%s"
ip_cidr_range = "10.1.0.0/16"
network = "${google_compute_network.network.name}"
}
resource "google_compute_instance" "test_vm" {
project = "%s"
zone = "%s"
name = "%s"
machine_type = "n1-standard-1"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
subnetwork ="${google_compute_subnetwork.subnetwork.self_link}"
}
}
resource "google_compute_instance_iam_member" "foo" {
project = "${google_compute_instance.test_vm.project}"
zone = "${google_compute_instance.test_vm.zone}"
instance_name = "${google_compute_instance.test_vm.name}"
role = "%s"
member = "serviceAccount:${google_service_account.test_account.email}"
}
`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId)
}
func testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string {
return fmt.Sprintf(`
resource "google_service_account" "test_account" {
account_id = "%s"
display_name = "Iam Testing Account"
}
resource "google_compute_network" "network" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnetwork" {
name = "%s"
region = "%s"
ip_cidr_range = "10.1.0.0/16"
network = "${google_compute_network.network.name}"
}
resource "google_compute_instance" "test_vm" {
project = "%s"
zone = "%s"
name = "%s"
machine_type = "n1-standard-1"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
subnetwork ="${google_compute_subnetwork.subnetwork.self_link}"
}
}
data "google_iam_policy" "foo" {
binding {
role = "%s"
members = ["serviceAccount:${google_service_account.test_account.email}"]
}
}
resource "google_compute_instance_iam_policy" "foo" {
project = "${google_compute_instance.test_vm.project}"
zone = "${google_compute_instance.test_vm.zone}"
instance_name = "${google_compute_instance.test_vm.name}"
policy_data = "${data.google_iam_policy.foo.policy_data}"
}
`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId)
}
func testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string {
return fmt.Sprintf(`
resource "google_service_account" "test_account" {
account_id = "%s"
display_name = "Iam Testing Account"
}
resource "google_compute_network" "network" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnetwork" {
name = "%s"
region = "%s"
ip_cidr_range = "10.1.0.0/16"
network = "${google_compute_network.network.name}"
}
resource "google_compute_instance" "test_vm" {
project = "%s"
zone = "%s"
name = "%s"
machine_type = "n1-standard-1"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
subnetwork ="${google_compute_subnetwork.subnetwork.self_link}"
}
}
resource "google_compute_instance_iam_binding" "foo" {
project = "${google_compute_instance.test_vm.project}"
zone = "${google_compute_instance.test_vm.zone}"
instance_name = "${google_compute_instance.test_vm.name}"
role = "%s"
members = ["serviceAccount:${google_service_account.test_account.email}"]
}
`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId)
}
func testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetworkName, instanceName, roleId string) string {
return fmt.Sprintf(`
resource "google_service_account" "test_account" {
account_id = "%s"
display_name = "Iam Testing Account"
}
resource "google_service_account" "test_account_2" {
account_id = "%s-2"
display_name = "Iam Testing Account"
}
resource "google_compute_network" "network" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnetwork" {
name = "%s"
region = "%s"
ip_cidr_range = "10.1.0.0/16"
network = "${google_compute_network.network.name}"
}
resource "google_compute_instance" "test_vm" {
project = "%s"
zone = "%s"
name = "%s"
machine_type = "n1-standard-1"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
subnetwork ="${google_compute_subnetwork.subnetwork.self_link}"
}
}
resource "google_compute_instance_iam_binding" "foo" {
project = "${google_compute_instance.test_vm.project}"
zone = "${google_compute_instance.test_vm.zone}"
instance_name = "${google_compute_instance.test_vm.name}"
role = "%s"
members = [
"serviceAccount:${google_service_account.test_account.email}",
"serviceAccount:${google_service_account.test_account_2.email}"
]
}
`, account, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId)
}