|
| 1 | +package fortiadc |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/Ouest-France/gofortiadc" |
| 5 | + "github.com/hashicorp/terraform/helper/schema" |
| 6 | +) |
| 7 | + |
| 8 | +func resourceFortiadcLoadbalanceContentRoutingCondition() *schema.Resource { |
| 9 | + return &schema.Resource{ |
| 10 | + Create: resourceFortiadcLoadbalanceContentRoutingConditionCreate, |
| 11 | + Read: resourceFortiadcLoadbalanceContentRoutingConditionRead, |
| 12 | + Update: resourceFortiadcLoadbalanceContentRoutingConditionUpdate, |
| 13 | + Delete: resourceFortiadcLoadbalanceContentRoutingConditionDelete, |
| 14 | + |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "content_routing": &schema.Schema{ |
| 17 | + Type: schema.TypeString, |
| 18 | + Required: true, |
| 19 | + ForceNew: true, |
| 20 | + }, |
| 21 | + "object": &schema.Schema{ |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "type": &schema.Schema{ |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "content": &schema.Schema{ |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + "reverse": &schema.Schema{ |
| 34 | + Type: schema.TypeBool, |
| 35 | + Optional: true, |
| 36 | + Default: false, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceFortiadcLoadbalanceContentRoutingConditionCreate(d *schema.ResourceData, m interface{}) error { |
| 43 | + client := m.(*gofortiadc.Client) |
| 44 | + |
| 45 | + reverse := "disable" |
| 46 | + if d.Get("reverse").(bool) { |
| 47 | + reverse = "enable" |
| 48 | + } |
| 49 | + |
| 50 | + req := gofortiadc.LoadbalanceContentRoutingCondition{ |
| 51 | + Mkey: "", |
| 52 | + Object: d.Get("object").(string), |
| 53 | + Type: d.Get("type").(string), |
| 54 | + Content: d.Get("content").(string), |
| 55 | + Reverse: reverse, |
| 56 | + } |
| 57 | + |
| 58 | + err := client.LoadbalanceCreateContentRoutingCondition(d.Get("content_routing").(string), req) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + id, err := client.LoadbalanceGetContentRoutingConditionID(d.Get("content_routing").(string), req) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + d.SetId(id) |
| 69 | + |
| 70 | + return resourceFortiadcLoadbalanceContentRoutingConditionRead(d, m) |
| 71 | +} |
| 72 | + |
| 73 | +func resourceFortiadcLoadbalanceContentRoutingConditionRead(d *schema.ResourceData, m interface{}) error { |
| 74 | + client := m.(*gofortiadc.Client) |
| 75 | + |
| 76 | + res, err := client.LoadbalanceGetContentRoutingCondition(d.Get("content_routing").(string), d.Id()) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + reverse := false |
| 82 | + if res.Reverse == "enable" { |
| 83 | + reverse = true |
| 84 | + } |
| 85 | + |
| 86 | + d.Set("object", res.Object) |
| 87 | + d.Set("type", res.Type) |
| 88 | + d.Set("content", res.Content) |
| 89 | + d.Set("reverse", reverse) |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func resourceFortiadcLoadbalanceContentRoutingConditionUpdate(d *schema.ResourceData, m interface{}) error { |
| 95 | + client := m.(*gofortiadc.Client) |
| 96 | + |
| 97 | + reverse := "disable" |
| 98 | + if d.Get("reverse").(bool) { |
| 99 | + reverse = "enable" |
| 100 | + } |
| 101 | + |
| 102 | + req := gofortiadc.LoadbalanceContentRoutingCondition{ |
| 103 | + Mkey: d.Id(), |
| 104 | + Object: d.Get("object").(string), |
| 105 | + Type: d.Get("type").(string), |
| 106 | + Content: d.Get("content").(string), |
| 107 | + Reverse: reverse, |
| 108 | + } |
| 109 | + |
| 110 | + err := client.LoadbalanceUpdateContentRoutingCondition(d.Get("content_routing").(string), req) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + return resourceFortiadcLoadbalanceContentRoutingConditionRead(d, m) |
| 116 | +} |
| 117 | + |
| 118 | +func resourceFortiadcLoadbalanceContentRoutingConditionDelete(d *schema.ResourceData, m interface{}) error { |
| 119 | + client := m.(*gofortiadc.Client) |
| 120 | + |
| 121 | + return client.LoadbalanceDeleteContentRoutingCondition(d.Get("content_routing").(string), d.Id()) |
| 122 | +} |
0 commit comments