|
| 1 | +package iis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/nrgribeiro/microsoft-iis-administration" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +const fileNameKey = "name" |
| 11 | +const filePhysicalPathKey = "physical_path" |
| 12 | +const typeKey = "type" |
| 13 | +const parentKey = "parent" |
| 14 | + |
| 15 | +func resourceFile() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + CreateContext: resourceFileCreate, |
| 18 | + ReadContext: resourceFileRead, |
| 19 | + UpdateContext: resourceFileUpdate, |
| 20 | + DeleteContext: resourceFileDelete, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + fileNameKey: { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + }, |
| 27 | + filePhysicalPathKey: { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + }, |
| 31 | + typeKey: { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + parentKey: { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func resourceFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 44 | + client := m.(*iis.Client) |
| 45 | + request := createFileRequest(d) |
| 46 | + site, err := client.CreateFile(ctx, request) |
| 47 | + if err != nil { |
| 48 | + return diag.FromErr(err) |
| 49 | + } |
| 50 | + d.SetId(site.ID) |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func resourceFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 55 | + client := m.(*iis.Client) |
| 56 | + site, err := client.ReadFile(ctx, d.Id()) |
| 57 | + if err != nil { |
| 58 | + d.SetId("") |
| 59 | + return diag.FromErr(err) |
| 60 | + } |
| 61 | + if err = d.Set(nameKey, site.Name); err != nil { |
| 62 | + return diag.FromErr(err) |
| 63 | + } |
| 64 | + if err = d.Set(physicalPathKey, site.PhysicalPath); err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + if err = d.Set(typeKey, site.Type); err != nil { |
| 68 | + return diag.FromErr(err) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func resourceFileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func resourceFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 78 | + client := m.(*iis.Client) |
| 79 | + id := d.Id() |
| 80 | + err := client.DeleteFile(ctx, id) |
| 81 | + if err != nil { |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func createFileRequest(d *schema.ResourceData) iis.CreateFileRequest { |
| 88 | + name := d.Get(nameKey).(string) |
| 89 | + physicalPath := d.Get(physicalPathKey).(string) |
| 90 | + parentId := d.Get(parentKey).(string) |
| 91 | + typeName := d.Get(typeKey).(string) |
| 92 | + request := iis.CreateFileRequest{ |
| 93 | + Name: name, |
| 94 | + PhysicalPath: physicalPath, |
| 95 | + Parent: iis.FileParent{ |
| 96 | + Id: parentId, |
| 97 | + }, |
| 98 | + Type: typeName, |
| 99 | + } |
| 100 | + |
| 101 | + return request |
| 102 | +} |
0 commit comments