Skip to content

Commit 5734238

Browse files
kms_key_name parameter for google_dataflow_job resource (#4359) (#2829)
Signed-off-by: Modular Magician <[email protected]>
1 parent a8b7a7e commit 5734238

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

.changelog/4359.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
dataflow: Added optional `kms_key_name` field for `google_dataflow_job`
3+
```

google-beta/resource_dataflow_job.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ func resourceDataflowJob() *schema.Resource {
171171
Description: `The machine type to use for the job.`,
172172
},
173173

174+
"kms_key_name": {
175+
Type: schema.TypeString,
176+
Optional: true,
177+
Description: `The name for the Cloud KMS key for the job. Key format is: projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY`,
178+
},
179+
174180
"ip_configuration": {
175181
Type: schema.TypeString,
176182
Optional: true,
@@ -303,6 +309,9 @@ func resourceDataflowJobRead(d *schema.ResourceData, meta interface{}) error {
303309
if err := d.Set("labels", job.Labels); err != nil {
304310
return fmt.Errorf("Error setting labels: %s", err)
305311
}
312+
if err := d.Set("kms_key_name", job.Environment.ServiceKmsKeyName); err != nil {
313+
return fmt.Errorf("Error setting kms_key_name: %s", err)
314+
}
306315

307316
sdkPipelineOptions, err := ConvertToMap(job.Environment.SdkPipelineOptions)
308317
if err != nil {
@@ -529,6 +538,7 @@ func resourceDataflowJobSetupEnv(d *schema.ResourceData, config *Config) (datafl
529538
Subnetwork: d.Get("subnetwork").(string),
530539
TempLocation: d.Get("temp_gcs_location").(string),
531540
MachineType: d.Get("machine_type").(string),
541+
KmsKeyName: d.Get("kms_key_name").(string),
532542
IpConfiguration: d.Get("ip_configuration").(string),
533543
AdditionalUserLabels: labels,
534544
Zone: zone,

google-beta/resource_dataflow_job_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ func TestAccDataflowJob_withIpConfig(t *testing.T) {
204204
})
205205
}
206206

207+
func TestAccDataflowJob_withKmsKey(t *testing.T) {
208+
// Dataflow responses include serialized java classes and bash commands
209+
// This makes body comparison infeasible
210+
skipIfVcr(t)
211+
t.Parallel()
212+
213+
randStr := randString(t, 10)
214+
key_ring := "tf-test-dataflow-kms-ring-" + randStr
215+
crypto_key := "tf-test-dataflow-kms-key-" + randStr
216+
bucket := "tf-test-dataflow-gcs-" + randStr
217+
job := "tf-test-dataflow-job-" + randStr
218+
zone := "us-central1-f"
219+
220+
vcrTest(t, resource.TestCase{
221+
PreCheck: func() { testAccPreCheck(t) },
222+
Providers: testAccProviders,
223+
CheckDestroy: testAccCheckDataflowJobDestroyProducer(t),
224+
Steps: []resource.TestStep{
225+
{
226+
Config: testAccDataflowJob_kms(key_ring, crypto_key, bucket, job, zone),
227+
Check: resource.ComposeTestCheckFunc(
228+
testAccDataflowJobExists(t, "google_dataflow_job.big_data"),
229+
),
230+
},
231+
},
232+
})
233+
}
207234
func TestAccDataflowJobWithAdditionalExperiments(t *testing.T) {
208235
// Dataflow responses include serialized java classes and bash commands
209236
// This makes body comparison infeasible
@@ -783,6 +810,57 @@ resource "google_dataflow_job" "with_labels" {
783810

784811
}
785812

813+
func testAccDataflowJob_kms(key_ring, crypto_key, bucket, job, zone string) string {
814+
return fmt.Sprintf(`
815+
data "google_project" "project" {
816+
}
817+
818+
resource "google_project_iam_member" "kms-project-dataflow-binding" {
819+
project = data.google_project.project.project_id
820+
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
821+
member = "serviceAccount:service-${data.google_project.project.number}@dataflow-service-producer-prod.iam.gserviceaccount.com"
822+
}
823+
824+
resource "google_project_iam_member" "kms-project-compute-binding" {
825+
project = data.google_project.project.project_id
826+
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
827+
member = "serviceAccount:service-${data.google_project.project.number}@compute-system.iam.gserviceaccount.com"
828+
}
829+
830+
resource "google_kms_key_ring" "keyring" {
831+
name = "%s"
832+
location = "global"
833+
}
834+
835+
resource "google_kms_crypto_key" "crypto_key" {
836+
name = "%s"
837+
key_ring = google_kms_key_ring.keyring.id
838+
rotation_period = "100000s"
839+
}
840+
841+
resource "google_storage_bucket" "temp" {
842+
name = "%s"
843+
force_destroy = true
844+
}
845+
846+
resource "google_dataflow_job" "big_data" {
847+
name = "%s"
848+
849+
zone = "%s"
850+
851+
machine_type = "e2-standard-2"
852+
template_gcs_path = "%s"
853+
temp_gcs_location = google_storage_bucket.temp.url
854+
kms_key_name = google_kms_crypto_key.crypto_key.self_link
855+
parameters = {
856+
inputFile = "%s"
857+
output = "${google_storage_bucket.temp.url}/output"
858+
}
859+
on_delete = "cancel"
860+
}
861+
`, key_ring, crypto_key, bucket, job, zone, testDataflowJobTemplateWordCountUrl, testDataflowJobSampleFileUrl)
862+
}
863+
786864
func testAccDataflowJob_additionalExperiments(bucket string, job string, experiments []string) string {
787865
return fmt.Sprintf(`
788866
resource "google_storage_bucket" "temp" {

website/docs/r/dataflow_job.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The following arguments are supported:
8787
* `network` - (Optional) The network to which VMs will be assigned. If it is not provided, "default" will be used.
8888
* `subnetwork` - (Optional) The subnetwork to which VMs will be assigned. Should be of the form "regions/REGION/subnetworks/SUBNETWORK".
8989
* `machine_type` - (Optional) The machine type to use for the job.
90+
* `kms_key_name` - (Optional) The name for the Cloud KMS key for the job. Key format is: `projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY`
9091
* `ip_configuration` - (Optional) The configuration for VM IPs. Options are `"WORKER_IP_PUBLIC"` or `"WORKER_IP_PRIVATE"`.
9192
* `additional_experiments` - (Optional) List of experiments that should be used by the job. An example value is `["enable_stackdriver_agent_metrics"]`.
9293

0 commit comments

Comments
 (0)