|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "cloud.google.com/go/bigtable" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + GCPolicyModeIntersection = "INTERSECTION" |
| 16 | + GCPolicyModeUnion = "UNION" |
| 17 | +) |
| 18 | + |
| 19 | +func resourceBigtableGCPolicy() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + Create: resourceBigtableGCPolicyCreate, |
| 22 | + Read: resourceBigtableGCPolicyRead, |
| 23 | + Delete: resourceBigtableGCPolicyDestroy, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "instance_name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + |
| 32 | + "table": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + }, |
| 37 | + |
| 38 | + "column_family": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + }, |
| 43 | + |
| 44 | + "mode": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + ForceNew: true, |
| 48 | + ValidateFunc: validation.StringInSlice([]string{GCPolicyModeIntersection, GCPolicyModeUnion}, false), |
| 49 | + }, |
| 50 | + |
| 51 | + "max_age": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Optional: true, |
| 54 | + ForceNew: true, |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "days": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Required: true, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + |
| 65 | + "max_version": { |
| 66 | + Type: schema.TypeList, |
| 67 | + Optional: true, |
| 68 | + ForceNew: true, |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "number": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Required: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + |
| 79 | + "project": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Optional: true, |
| 82 | + Computed: true, |
| 83 | + ForceNew: true, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func resourceBigtableGCPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 90 | + config := meta.(*Config) |
| 91 | + ctx := context.Background() |
| 92 | + |
| 93 | + project, err := getProject(d, config) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + instanceName := d.Get("instance_name").(string) |
| 99 | + c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("Error starting admin client. %s", err) |
| 102 | + } |
| 103 | + |
| 104 | + defer c.Close() |
| 105 | + |
| 106 | + gcPolicy, err := generateBigtableGCPolicy(d) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + tableName := d.Get("table").(string) |
| 112 | + columnFamily := d.Get("column_family").(string) |
| 113 | + |
| 114 | + if err := c.SetGCPolicy(ctx, tableName, columnFamily, gcPolicy); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + table, err := c.TableInfo(ctx, tableName) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("Error retrieving table. Could not find %s in %s. %s", tableName, instanceName, err) |
| 121 | + } |
| 122 | + |
| 123 | + for _, i := range table.FamilyInfos { |
| 124 | + if i.Name == columnFamily { |
| 125 | + d.SetId(i.GCPolicy) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return resourceBigtableGCPolicyRead(d, meta) |
| 130 | +} |
| 131 | + |
| 132 | +func resourceBigtableGCPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + config := meta.(*Config) |
| 134 | + ctx := context.Background() |
| 135 | + |
| 136 | + project, err := getProject(d, config) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + instanceName := d.Get("instance_name").(string) |
| 142 | + c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("Error starting admin client. %s", err) |
| 145 | + } |
| 146 | + |
| 147 | + defer c.Close() |
| 148 | + |
| 149 | + name := d.Get("table").(string) |
| 150 | + ti, err := c.TableInfo(ctx, name) |
| 151 | + if err != nil { |
| 152 | + log.Printf("[WARN] Removing %s because it's gone", name) |
| 153 | + d.SetId("") |
| 154 | + return fmt.Errorf("Error retrieving table. Could not find %s in %s. %s", name, instanceName, err) |
| 155 | + } |
| 156 | + |
| 157 | + for _, fi := range ti.FamilyInfos { |
| 158 | + if fi.Name == name { |
| 159 | + d.SetId(fi.GCPolicy) |
| 160 | + break |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + d.Set("project", project) |
| 165 | + |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +func resourceBigtableGCPolicyDestroy(d *schema.ResourceData, meta interface{}) error { |
| 170 | + config := meta.(*Config) |
| 171 | + ctx := context.Background() |
| 172 | + |
| 173 | + project, err := getProject(d, config) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + instanceName := d.Get("instance_name").(string) |
| 179 | + c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) |
| 180 | + if err != nil { |
| 181 | + return fmt.Errorf("Error starting admin client. %s", err) |
| 182 | + } |
| 183 | + |
| 184 | + defer c.Close() |
| 185 | + |
| 186 | + if err := c.SetGCPolicy(ctx, d.Get("table").(string), d.Get("column_family").(string), bigtable.NoGcPolicy()); err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + d.SetId("") |
| 191 | + |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error) { |
| 196 | + var policies []bigtable.GCPolicy |
| 197 | + mode := d.Get("mode").(string) |
| 198 | + ma, aok := d.GetOk("max_age") |
| 199 | + mv, vok := d.GetOk("max_version") |
| 200 | + |
| 201 | + if !aok && !vok { |
| 202 | + return bigtable.NoGcPolicy(), nil |
| 203 | + } |
| 204 | + |
| 205 | + if mode == "" && aok && vok { |
| 206 | + return nil, fmt.Errorf("If multiple policies are set, mode can't be empty") |
| 207 | + } |
| 208 | + |
| 209 | + if aok { |
| 210 | + l, _ := ma.([]interface{}) |
| 211 | + d, _ := l[0].(map[string]interface{})["days"].(int) |
| 212 | + |
| 213 | + policies = append(policies, bigtable.MaxAgePolicy(time.Duration(d)*time.Hour*24)) |
| 214 | + } |
| 215 | + |
| 216 | + if vok { |
| 217 | + l, _ := mv.([]interface{}) |
| 218 | + n, _ := l[0].(map[string]interface{})["number"].(int) |
| 219 | + |
| 220 | + policies = append(policies, bigtable.MaxVersionsPolicy(n)) |
| 221 | + } |
| 222 | + |
| 223 | + switch mode { |
| 224 | + case GCPolicyModeUnion: |
| 225 | + return bigtable.UnionPolicy(policies...), nil |
| 226 | + case GCPolicyModeIntersection: |
| 227 | + return bigtable.IntersectionPolicy(policies...), nil |
| 228 | + } |
| 229 | + |
| 230 | + return policies[0], nil |
| 231 | +} |
0 commit comments