|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +var projectConnectionSchema = map[string]*schema.Schema{ |
| 16 | + "connection_id": &schema.Schema{ |
| 17 | + Type: schema.TypeInt, |
| 18 | + Required: true, |
| 19 | + Description: "Connection ID", |
| 20 | + }, |
| 21 | + "project_id": &schema.Schema{ |
| 22 | + Type: schema.TypeInt, |
| 23 | + Required: true, |
| 24 | + Description: "Project ID", |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +func ResourceProjectConnection() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + CreateContext: resourceProjectConnectionCreate, |
| 31 | + ReadContext: resourceProjectConnectionRead, |
| 32 | + UpdateContext: resourceProjectConnectionUpdate, |
| 33 | + DeleteContext: resourceProjectConnectionDelete, |
| 34 | + |
| 35 | + CustomizeDiff: customdiff.All( |
| 36 | + customdiff.ForceNewIfChange("connection_id", func(_ context.Context, old, new, meta interface{}) bool { return true }), |
| 37 | + customdiff.ForceNewIfChange("project_id", func(_ context.Context, old, new, meta interface{}) bool { return true }), |
| 38 | + ), |
| 39 | + |
| 40 | + Schema: projectConnectionSchema, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + StateContext: schema.ImportStatePassthroughContext, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func resourceProjectConnectionCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 48 | + c := m.(*dbt_cloud.Client) |
| 49 | + |
| 50 | + var diags diag.Diagnostics |
| 51 | + |
| 52 | + connectionID := d.Get("connection_id").(int) |
| 53 | + projectID := d.Get("project_id").(int) |
| 54 | + projectIDString := strconv.Itoa(projectID) |
| 55 | + |
| 56 | + project, err := c.GetProject(projectIDString) |
| 57 | + if err != nil { |
| 58 | + return diag.FromErr(err) |
| 59 | + } |
| 60 | + |
| 61 | + project.ConnectionID = &connectionID |
| 62 | + |
| 63 | + _, err = c.UpdateProject(projectIDString, *project) |
| 64 | + if err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + d.SetId(fmt.Sprintf("%d%s%d", *project.ID, dbt_cloud.ID_DELIMITER, project.ConnectionID)) |
| 69 | + |
| 70 | + resourceProjectConnectionRead(ctx, d, m) |
| 71 | + |
| 72 | + return diags |
| 73 | +} |
| 74 | + |
| 75 | +func resourceProjectConnectionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 76 | + c := m.(*dbt_cloud.Client) |
| 77 | + |
| 78 | + var diags diag.Diagnostics |
| 79 | + |
| 80 | + projectID, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]) |
| 81 | + if err != nil { |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + projectIDString := strconv.Itoa(projectID) |
| 85 | + |
| 86 | + project, err := c.GetProject(projectIDString) |
| 87 | + if err != nil { |
| 88 | + return diag.FromErr(err) |
| 89 | + } |
| 90 | + |
| 91 | + if err := d.Set("connection_id", project.ConnectionID); err != nil { |
| 92 | + return diag.FromErr(err) |
| 93 | + } |
| 94 | + if err := d.Set("project_id", project.ID); err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + |
| 98 | + return diags |
| 99 | +} |
| 100 | + |
| 101 | +func resourceProjectConnectionUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 102 | + |
| 103 | + return resourceProjectConnectionRead(ctx, d, m) |
| 104 | +} |
| 105 | + |
| 106 | +func resourceProjectConnectionDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 107 | + c := m.(*dbt_cloud.Client) |
| 108 | + |
| 109 | + var diags diag.Diagnostics |
| 110 | + |
| 111 | + projectID := d.Get("project_id").(int) |
| 112 | + projectIDString := strconv.Itoa(projectID) |
| 113 | + |
| 114 | + project, err := c.GetProject(projectIDString) |
| 115 | + if err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + |
| 119 | + project.ConnectionID = nil |
| 120 | + |
| 121 | + _, err = c.UpdateProject(projectIDString, *project) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + return diags |
| 127 | +} |
0 commit comments