forked from houqp/terraform-provider-airflow
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from DrFaust92/dag_run_id
Support DAG Runs
- Loading branch information
Showing
12 changed files
with
333 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
layout: "airflow" | ||
page_title: "Airflow: airflow_dag_run" | ||
sidebar_current: "docs-airflow-resource-dag-run" | ||
description: |- | ||
Provides an Airflow dag run | ||
--- | ||
|
||
# airflow_dag_run | ||
|
||
Provides an Airflow dag run. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "airflow_dag_run" "example" { | ||
dag_id = "example" | ||
dag_run_id = "example" | ||
conf = { | ||
"example" = "example" | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `dag_id` - (Required) The DAG ID to run. | ||
* `dag_run_id` - (Optional) The DAG Run ID. If a value is not passed, a random one will be generated based on execution date. | ||
* `conf` - (Optional) A map describing additional configuration parameters. | ||
|
||
## Attributes Reference | ||
|
||
This resource exports the following attributes: | ||
|
||
* `id` - The `dag_id:dag_run_id`. | ||
* `state` - The DAG state. | ||
|
||
## Import | ||
|
||
DAG Runs can be imported using the `dag_id:dag_run_id`. | ||
|
||
```terraform | ||
terraform import airflow_dag_run.default example:example | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/apache/airflow-client-go/airflow" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceDagRun() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceDagRunCreate, | ||
Read: resourceDagRunRead, | ||
Delete: resourceDagRunDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"dag_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"dag_run_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
"conf": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDagRunCreate(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient.DAGRunApi | ||
|
||
dagId := d.Get("dag_id").(string) | ||
// dagRunId := d.Get("dag_run_id").(string) | ||
|
||
dagRun := *airflow.NewDAGRunWithDefaults() | ||
|
||
if v, ok := d.GetOk("dag_run_id"); ok { | ||
dagRun.SetDagRunId(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("conf"); ok { | ||
dagRun.SetConf(v.(map[string]interface{})) | ||
} | ||
|
||
res, _, err := client.PostDagRun(pcfg.AuthContext, dagId).DAGRun(dagRun).Execute() | ||
if err != nil { | ||
return fmt.Errorf("failed to create dagRunId `%s` from Airflow: %w", dagId, err) | ||
} | ||
d.SetId(fmt.Sprintf("%s:%s", dagId, *res.DagRunId.Get())) | ||
|
||
return resourceDagRunRead(d, m) | ||
} | ||
|
||
func resourceDagRunRead(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient.DAGRunApi | ||
|
||
dagId, dagRunId, err := airflowDagRunId(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dagRun, resp, err := client.GetDagRun(pcfg.AuthContext, dagId, dagRunId).Execute() | ||
if resp != nil && resp.StatusCode == 404 { | ||
d.SetId("") | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to get dagRunId `%s` from Airflow: %w", d.Id(), err) | ||
} | ||
|
||
d.Set("dag_id", dagRun.DagId) | ||
d.Set("dag_run_id", dagRun.DagRunId.Get()) | ||
d.Set("conf", dagRun.Conf) | ||
d.Set("state", dagRun.State) | ||
|
||
return nil | ||
} | ||
|
||
func resourceDagRunDelete(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient.DAGRunApi | ||
|
||
dagId, dagRunId, err := airflowDagRunId(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.DeleteDagRun(pcfg.AuthContext, dagId, dagRunId).Execute() | ||
if err != nil { | ||
return fmt.Errorf("failed to delete dagRunId `%s` from Airflow: %w", d.Id(), err) | ||
} | ||
|
||
if resp != nil && resp.StatusCode == 404 { | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func airflowDagRunId(id string) (string, string, error) { | ||
parts := strings.SplitN(id, ":", 2) | ||
|
||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
return "", "", fmt.Errorf("unexpected format of ID (%s), expected DAG-ID:DAG-RUN-ID", id) | ||
} | ||
|
||
return parts[0], parts[1], nil | ||
} |
Oops, something went wrong.