Skip to content

Commit

Permalink
Merge pull request #19 from niharika-98/bp-bpa-updated
Browse files Browse the repository at this point in the history
Adding datasource for BP and BPA
  • Loading branch information
niharika-98 authored Oct 17, 2024
2 parents c39a9ce + 7ccf87a commit 8c434e7
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mmv1/products/backupdr/BackupPlanAssociation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ properties:
output: true
properties:
- name: 'code'
type: String
type: Double
description: The status code, which should be an enum value of [google.rpc.Code]
- name: 'message'
type: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
{{- if ne $.TargetVersionName "ga" }}
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
"google_backup_dr_backup_plan": backupdr.DataSourceGoogleCloudBackupDRBackupPlan(),
"google_backup_dr_backup_plan_association": backupdr.DataSourceGoogleCloudBackupDRBackupPlanAssociation(),
{{- end }}
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package backupdr
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleCloudBackupDRBackupPlan() *schema.Resource {

dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRBackupPlan().Schema)
// Set 'Required' schema elements
tpgresource.AddRequiredFieldsToSchema(dsSchema, "backup_plan_id", "location")

// Set 'Optional' schema elements
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
return &schema.Resource{
Read: dataSourceGoogleCloudBackupDRBackupPlanRead,
Schema: dsSchema,
}
}

func dataSourceGoogleCloudBackupDRBackupPlanRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)

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

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

backup_plan_id := d.Get("backup_plan_id").(string)

id := fmt.Sprintf("projects/%s/locations/%s/backupPlans/%s", project, location, backup_plan_id)
d.SetId(id)
err = resourceBackupDRBackupPlanRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package backupdr
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleCloudBackupDRBackupPlanAssociation() *schema.Resource {

dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRBackupPlanAssociation().Schema)
// Set 'Required' schema elements
tpgresource.AddRequiredFieldsToSchema(dsSchema, "backup_plan_association_id", "location")

// Set 'Optional' schema elements
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
return &schema.Resource{
Read: dataSourceGoogleCloudBackupDRBackupPlanAssociationRead,
Schema: dsSchema,
}
}

func dataSourceGoogleCloudBackupDRBackupPlanAssociationRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)

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

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

backup_plan_association_id := d.Get("backup_plan_association_id").(string)

id := fmt.Sprintf("projects/%s/locations/%s/backupPlanAssociations/%s", project, location, backup_plan_association_id)
d.SetId(id)
err = resourceBackupDRBackupPlanAssociationRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package backupdr_test
{{- if ne $.TargetVersionName "ga" }}

import (
"testing"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"github.com/hashicorp/terraform-plugin-testing/terraform"

)

func TestAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceState("data.google_backup_dr_backup_plan_association.bpa", "google_backup_dr_backup_plan_association.bpa-data"),
),
},
},
})
}

func testAccCheckBackupDRBackupPlanAssociationDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_backup_dr_backup_plan" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

config := acctest.GoogleProviderConfig(t)

url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}BackupDRBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/backupPlans/{{"{{"}}backup_plan_id{{"}}"}}")
if err != nil {
return err
}

billingProject := ""

if config.BillingProject != "" {
billingProject = config.BillingProject
}

_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
})
if err == nil {
return fmt.Errorf("Backup Plan still exists at %s", url)
}
}

return nil
}
}


func testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_service_account" "default" {
account_id = "my-custom-sa"
display_name = "Custom SA for VM Instance"
}

resource "google_compute_instance" "default" {
name = "test"
machine_type = "n2-standard-2"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}
// Local SSD disk
scratch_disk {
interface = "NVME"
}
network_interface {
network = "default"
access_config {
// Ephemeral public IP
}
}
service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
resource "google_backup_dr_backup_vault" "my-backup-vault" {
location ="us-central1"
backup_vault_id = "bv-1"
description = "This is a second backup vault built by Terraform."
backup_minimum_enforced_retention_duration = "100000s"
labels = {
foo = "bar1"
bar = "baz1"
}
annotations = {
annotations1 = "bar1"
annotations2 = "baz1"
}
force_update = "true"
force_delete = "true"
allow_missing = "true"
}


resource "google_backup_dr_backup_plan" "foo" {
location = "us-central1"
backup_plan_id = "bp-test-tf"
resource_type= "compute.googleapis.com/Instance"
backup_vault = google_backup_dr_backup_vault.my-backup-vault.name
depends_on= [ google_backup_dr_backup_vault.my-backup-vault ]
backup_rules {
rule_id = "rule-1"
backup_retention_days = 5
standard_schedule {
recurrence_type = "HOURLY"
hourly_frequency = 6
time_zone = "UTC"
backup_window{
start_hour_of_day = 0
end_hour_of_day = 24
}
}
}
}

resource "google_backup_dr_backup_plan_association" "bpa" {
location = "us-central1"
backup_plan_association_id = "bpa-test-tf"
resource = google_compute_instance.default.id
backup_plan = google_backup_dr_backup_plan.foo.name
depends_on= [ google_backup_dr_backup_plan.foo ]

}

data "google_backup_dr_backup_plan_association" "bpa-data" {
location = "us-central1"
backup_plan_association_id="bpa-test-tf1"
depends_on= [ google_backup_dr_backup_plan_association.bpa ]
}
`, context)
}
{{- end }}
Loading

0 comments on commit 8c434e7

Please sign in to comment.