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 #2 from DrFaust92/sdk-v2
sdk v2
- Loading branch information
Showing
10 changed files
with
392 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
TEST?=$$(go list ./...) | ||
|
||
build: | ||
go build -o terraform-provider-airflow | ||
|
||
testacc: | ||
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 5m |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/plugin" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: func() terraform.ResourceProvider { | ||
return Provider() | ||
ProviderFunc: func() *schema.Provider { | ||
return AirflowProvider() | ||
}, | ||
}) | ||
} |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var testAccProviders map[string]*schema.Provider | ||
var testAccProvider *schema.Provider | ||
|
||
func init() { | ||
testAccProvider = AirflowProvider() | ||
testAccProviders = map[string]*schema.Provider{ | ||
"airflow": testAccProvider, | ||
} | ||
} | ||
|
||
func TestProvider(t *testing.T) { | ||
if err := AirflowProvider().InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func TestProvider_impl(t *testing.T) { | ||
var _ *schema.Provider = AirflowProvider() | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
if v := os.Getenv("AIRFLOW_OAUTH2_TOKEN"); v == "" { | ||
t.Fatal("AIRFLOW_OAUTH2_TOKEN must be set for acceptance tests") | ||
} | ||
if v := os.Getenv("AIRFLOW_BASE_ENDPOINT"); v == "" { | ||
t.Fatal("AIRFLOW_BASE_ENDPOINT must be set for acceptance tests") | ||
} | ||
} |
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,121 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/apache/airflow-client-go/airflow" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceConnectionCreate(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient | ||
connId := d.Get("connection_id").(string) | ||
connType := d.Get("conn_type").(string) | ||
|
||
conn := airflow.Connection{ | ||
ConnectionId: &connId, | ||
ConnType: &connType, | ||
} | ||
connApi := client.ConnectionApi | ||
|
||
_, _, err := connApi.PostConnection(pcfg.AuthContext).Connection(conn).Execute() | ||
if err != nil { | ||
return fmt.Errorf("failed to create connection `%s` from Airflow: %w", connId, err) | ||
} | ||
d.SetId(connId) | ||
return resourceConnectionRead(d, m) | ||
} | ||
|
||
func resourceConnectionRead(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient | ||
connId := d.Get("connection_id").(string) | ||
connection, resp, err := client.ConnectionApi.GetConnection(pcfg.AuthContext, connId).Execute() | ||
if resp != nil && resp.StatusCode == 404 { | ||
d.SetId("") | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to get connection `%s` from Airflow: %w", connId, err) | ||
} | ||
|
||
d.Set("connection_id", connection.ConnectionId) | ||
d.Set("conn_type", connection.ConnType) | ||
return nil | ||
} | ||
|
||
func resourceConnectionUpdate(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient | ||
connId := d.Get("connection_id").(string) | ||
connType := d.Get("conn_type").(string) | ||
|
||
conn := airflow.Connection{ | ||
ConnectionId: &connId, | ||
ConnType: &connType, | ||
} | ||
_, _, err := client.ConnectionApi.PatchConnection(pcfg.AuthContext, connId).Connection(conn).Execute() | ||
if err != nil { | ||
return fmt.Errorf("failed to update connection `%s` from Airflow: %w", connId, err) | ||
} | ||
d.SetId(connId) | ||
return resourceConnectionRead(d, m) | ||
} | ||
|
||
func resourceConnectionDelete(d *schema.ResourceData, m interface{}) error { | ||
pcfg := m.(ProviderConfig) | ||
client := pcfg.ApiClient | ||
connId := d.Get("connection_id").(string) | ||
_, err := client.ConnectionApi.DeleteConnection(pcfg.AuthContext, connId).Execute() | ||
if err != nil { | ||
return fmt.Errorf("failed to delete connection `%s` from Airflow: %w", connId, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceConnection() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceConnectionCreate, | ||
Read: resourceConnectionRead, | ||
Update: resourceConnectionUpdate, | ||
Delete: resourceConnectionDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"connection_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"conn_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"host": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"login": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"schema": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"port": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"password": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Sensitive: true, | ||
}, | ||
"extra": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} |
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,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccAirflowConnection_basic(t *testing.T) { | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resourceName := "airflow_connection.test" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAirflowConnectionConfigBasic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "connection_id", rName), | ||
resource.TestCheckResourceAttr(resourceName, "conn_type", rName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAirflowConnectionConfigBasic(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "airflow_connection" "test" { | ||
connection_id = %[1]q | ||
conn_type = %[1]q | ||
} | ||
`, rName) | ||
} |
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
Oops, something went wrong.