|
| 1 | +package jumpcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2" |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceGroupsSystem() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Create: resourceGroupsSystemCreate, |
| 14 | + Read: resourceGroupsSystemRead, |
| 15 | + Update: resourceGroupsSystemUpdate, |
| 16 | + Delete: resourceGroupsSystemDelete, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "name": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + }, |
| 22 | + "jc_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + }, |
| 27 | + Importer: &schema.ResourceImporter{ |
| 28 | + State: schema.ImportStatePassthrough, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func resourceGroupsSystemCreate(d *schema.ResourceData, m interface{}) error { |
| 34 | + config := m.(*jcapiv2.Configuration) |
| 35 | + client := jcapiv2.NewAPIClient(config) |
| 36 | + |
| 37 | + body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)} |
| 38 | + |
| 39 | + req := map[string]interface{}{ |
| 40 | + "body": body, |
| 41 | + } |
| 42 | + group, res, err := client.SystemGroupsApi.GroupsSystemPost(context.TODO(), |
| 43 | + "", headerAccept, req) |
| 44 | + if err != nil { |
| 45 | + // TODO: sort out error essentials |
| 46 | + return fmt.Errorf("error creating system group %s: %s - response = %+v", |
| 47 | + (req["body"].(jcapiv2.SystemGroupData)).Name, err, res) |
| 48 | + } |
| 49 | + |
| 50 | + d.SetId(group.Name) |
| 51 | + d.Set("name", group.Name) |
| 52 | + d.Set("jc_id", group.Id) |
| 53 | + return resourceGroupsSystemRead(d, m) |
| 54 | +} |
| 55 | + |
| 56 | +// Helper to look up a system group by name |
| 57 | +func resourceGroupsSystemList_match(d *schema.ResourceData, m interface{}) (jcapiv2.SystemGroup, error) { |
| 58 | + config := m.(*jcapiv2.Configuration) |
| 59 | + client := jcapiv2.NewAPIClient(config) |
| 60 | + |
| 61 | + var filter []string |
| 62 | + |
| 63 | + filter = append(filter, "name:eq:"+d.Id()) |
| 64 | + |
| 65 | + optional := map[string]interface{}{ |
| 66 | + "filter": filter, |
| 67 | + } |
| 68 | + |
| 69 | + result, _, err := client.SystemGroupsApi.GroupsSystemList(context.TODO(), |
| 70 | + "", headerAccept, optional) |
| 71 | + if err == nil { |
| 72 | + if len(result) < 1 { |
| 73 | + return jcapiv2.SystemGroup{}, fmt.Errorf("System Group \"%s\" not found.", d.Id()) |
| 74 | + } else { |
| 75 | + return result[0], nil |
| 76 | + } |
| 77 | + } else { |
| 78 | + return jcapiv2.SystemGroup{}, err |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func resourceGroupsSystemRead(d *schema.ResourceData, m interface{}) error { |
| 83 | + config := m.(*jcapiv2.Configuration) |
| 84 | + client := jcapiv2.NewAPIClient(config) |
| 85 | + |
| 86 | + var id string |
| 87 | + |
| 88 | + id = d.Get("jc_id").(string) |
| 89 | + |
| 90 | + if id == "" { |
| 91 | + id_lookup, err := resourceGroupsSystemList_match(d, m) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("Unable to locate ID for group %s, %+v", |
| 94 | + d.Get("name"), err) |
| 95 | + } |
| 96 | + id = id_lookup.Id |
| 97 | + d.SetId(id_lookup.Name) |
| 98 | + d.Set("name", id_lookup.Name) |
| 99 | + d.Set("jc_id", id_lookup.Id) |
| 100 | + } |
| 101 | + |
| 102 | + group, res, err := client.SystemGroupsApi.GroupsSystemGet(context.TODO(), |
| 103 | + id, "", headerAccept, nil) |
| 104 | + if err != nil { |
| 105 | + // TODO: sort out error essentials |
| 106 | + return fmt.Errorf("error reading system group ID %s: %s - response = %+v", |
| 107 | + d.Id(), err, res) |
| 108 | + } |
| 109 | + |
| 110 | + d.SetId(group.Name) |
| 111 | + d.Set("name", group.Name) |
| 112 | + d.Set("jc_id", group.Id) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func resourceGroupsSystemUpdate(d *schema.ResourceData, m interface{}) error { |
| 117 | + config := m.(*jcapiv2.Configuration) |
| 118 | + client := jcapiv2.NewAPIClient(config) |
| 119 | + |
| 120 | + var id string |
| 121 | + id = d.Get("jc_id").(string) |
| 122 | + |
| 123 | + body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)} |
| 124 | + |
| 125 | + req := map[string]interface{}{ |
| 126 | + "body": body, |
| 127 | + } |
| 128 | + |
| 129 | + group, res, err := client.SystemGroupsApi.GroupsSystemPut(context.TODO(), |
| 130 | + id, "", headerAccept, req) |
| 131 | + if err != nil { |
| 132 | + // TODO: sort out error essentials |
| 133 | + return fmt.Errorf("error updating system group %s: %s - response = %+v", |
| 134 | + d.Get("name"), err, res) |
| 135 | + } |
| 136 | + |
| 137 | + d.SetId(group.Name) |
| 138 | + d.Set("name", group.Name) |
| 139 | + d.Set("jc_id", group.Id) |
| 140 | + return resourceGroupsSystemRead(d, m) |
| 141 | +} |
| 142 | + |
| 143 | +func resourceGroupsSystemDelete(d *schema.ResourceData, m interface{}) error { |
| 144 | + config := m.(*jcapiv2.Configuration) |
| 145 | + client := jcapiv2.NewAPIClient(config) |
| 146 | + |
| 147 | + var id string |
| 148 | + id = d.Get("jc_id").(string) |
| 149 | + |
| 150 | + res, err := client.SystemGroupsApi.GroupsSystemDelete(context.TODO(), |
| 151 | + id, "", headerAccept, nil) |
| 152 | + if err != nil { |
| 153 | + // TODO: sort out error essentials |
| 154 | + return fmt.Errorf("error deleting system group:%s; response = %+v", err, res) |
| 155 | + } |
| 156 | + d.SetId("") |
| 157 | + return nil |
| 158 | +} |
0 commit comments