Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 28 additions & 41 deletions resource_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ func resourceConnection() *schema.Resource {
ValidateFunc: validation.IsPortNumberOrZero,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return old == ""
},
Type: schema.TypeString,
Optional: true,
},
"extra": {
Type: schema.TypeString,
Expand All @@ -72,33 +68,25 @@ func resourceConnectionCreate(d *schema.ResourceData, m interface{}) error {
}

if v, ok := d.GetOk("host"); ok {
val := v.(string)
conn.Host = *airflow.NewNullableString(&val)
conn.SetHost(v.(string))
}

if v, ok := d.GetOk("login"); ok {
val := v.(string)
conn.Login = *airflow.NewNullableString(&val)
conn.SetLogin(v.(string))
}

if v, ok := d.GetOk("schema"); ok {
val := v.(string)
conn.Schema = *airflow.NewNullableString(&val)
conn.SetSchema(v.(string))
}

if v, ok := d.GetOk("port"); ok {
val := int32(v.(int))
conn.Port = *airflow.NewNullableInt32(&val)
conn.SetPort(int32(v.(int)))
}

if v, ok := d.GetOk("password"); ok {
val := v.(string)
conn.Password = &val
}
conn.SetPassword(d.Get("password").(string))

if v, ok := d.GetOk("extra"); ok {
val := v.(string)
conn.Extra = *airflow.NewNullableString(&val)
conn.SetExtra(v.(string))
}

connApi := client.ConnectionApi
Expand All @@ -124,14 +112,19 @@ func resourceConnectionRead(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("failed to get connection `%s` from Airflow: %w", d.Id(), err)
}

d.Set("connection_id", connection.ConnectionId)
d.Set("conn_type", connection.ConnType)
d.Set("host", connection.Host.Get())
d.Set("login", connection.Login.Get())
d.Set("schema", connection.Schema.Get())
d.Set("port", connection.Port.Get())
d.Set("password", connection.Password)
d.Set("extra", connection.Extra.Get())
d.Set("connection_id", connection.GetConnectionId())
d.Set("conn_type", connection.GetConnType())
d.Set("host", connection.GetHost())
d.Set("login", connection.GetLogin())
d.Set("schema", connection.GetSchema())
d.Set("port", connection.GetPort())
d.Set("extra", connection.GetExtra())

if v, ok := connection.GetPasswordOk(); ok {
d.Set("password", v)
} else if v, ok := d.GetOk("password"); ok {
d.Set("password", v)
}

return nil
}
Expand All @@ -148,33 +141,27 @@ func resourceConnectionUpdate(d *schema.ResourceData, m interface{}) error {
}

if v, ok := d.GetOk("host"); ok {
val := v.(string)
conn.Host = *airflow.NewNullableString(&val)
conn.SetHost(v.(string))
}

if v, ok := d.GetOk("login"); ok {
val := v.(string)
conn.Login = *airflow.NewNullableString(&val)
conn.SetLogin(v.(string))
}

if v, ok := d.GetOk("schema"); ok {
val := v.(string)
conn.Schema = *airflow.NewNullableString(&val)
conn.SetSchema(v.(string))
}

if v, ok := d.GetOk("port"); ok {
val := int32(v.(int))
conn.Port = *airflow.NewNullableInt32(&val)
conn.SetPort(int32(v.(int)))
}

if v, ok := d.GetOk("password"); ok {
val := v.(string)
conn.Password = &val
if v, ok := d.GetOk("password"); ok && v.(string) != "" {
conn.SetPassword(v.(string))
}

if v, ok := d.GetOk("extra"); ok {
val := v.(string)
conn.Extra = *airflow.NewNullableString(&val)
conn.SetExtra(v.(string))
}

_, _, err := client.ConnectionApi.PatchConnection(pcfg.AuthContext, connId).Connection(conn).Execute()
Expand Down
21 changes: 11 additions & 10 deletions resource_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAccAirflowConnection_basic(t *testing.T) {
Config: testAccAirflowConnectionConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "connection_id", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", "http"),
),
},
{
Expand All @@ -48,31 +48,32 @@ func TestAccAirflowConnection_full(t *testing.T) {
Config: testAccAirflowConnectionConfigFull(rName, rName, 443),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "connection_id", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", "http"),
resource.TestCheckResourceAttr(resourceName, "host", rName),
resource.TestCheckResourceAttr(resourceName, "login", rName),
resource.TestCheckResourceAttr(resourceName, "schema", rName),
resource.TestCheckResourceAttr(resourceName, "port", "443"),
// resource.TestCheckResourceAttr(resourceName, "password", rName),
resource.TestCheckResourceAttr(resourceName, "extra", rName),
resource.TestCheckResourceAttr(resourceName, "password", rName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
{
Config: testAccAirflowConnectionConfigFull(rName, rNameUpdated, 80),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "connection_id", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", rName),
resource.TestCheckResourceAttr(resourceName, "conn_type", "http"),
resource.TestCheckResourceAttr(resourceName, "host", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "login", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "schema", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "port", "80"),
// resource.TestCheckResourceAttr(resourceName, "password", rName),
resource.TestCheckResourceAttr(resourceName, "extra", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "password", rNameUpdated),
),
},
},
Expand Down Expand Up @@ -106,7 +107,7 @@ func testAccAirflowConnectionConfigBasic(rName string) string {
return fmt.Sprintf(`
resource "airflow_connection" "test" {
connection_id = %[1]q
conn_type = %[1]q
conn_type = "http"
}
`, rName)
}
Expand All @@ -115,7 +116,7 @@ func testAccAirflowConnectionConfigFull(rName, rName2 string, port int) string {
return fmt.Sprintf(`
resource "airflow_connection" "test" {
connection_id = %[1]q
conn_type = %[1]q
conn_type = "http"
host = %[2]q
login = %[2]q
schema = %[2]q
Expand Down