-
Notifications
You must be signed in to change notification settings - Fork 117
/
resource_bigip_fast_template.go
133 lines (118 loc) · 3.65 KB
/
resource_bigip_fast_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package bigip
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
bigip "github.com/f5devcentral/go-bigip"
"github.com/f5devcentral/go-bigip/f5teem"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceBigipFastTemplate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBigipFastCreate,
ReadContext: resourceBigipFastRead,
UpdateContext: resourceBigipFastUpdate,
DeleteContext: resourceBigipFastDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Name of Fast template set",
},
"source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Location of the fast template set package on disk",
},
"md5_hash": {
Type: schema.TypeString,
Required: true,
Description: "MD5 hash of the fast template zip file",
},
},
}
}
func resourceBigipFastCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
tmplPath := d.Get("source").(string)
tmplName := filepath.Base(tmplPath)
checksum := d.Get("md5_hash").(string)
var name string
if _, ok := d.GetOk("name"); ok {
name = d.Get("name").(string)
} else {
name = strings.TrimSuffix(tmplName, ".zip")
}
log.Println("[INFO] Creating Fast Template Name " + name)
log.Println("[INFO] Reading provided archive " + tmplName)
file, fail := os.OpenFile(tmplPath, os.O_RDWR, 0644)
if fail != nil {
return diag.FromErr(fmt.Errorf("error in reading file: %s", fail))
}
err := client.UploadFastTemplate(file, name)
defer file.Close()
if err != nil {
return diag.FromErr(fmt.Errorf("error in creating FAST template set (%s): %s", name, err))
}
_ = d.Set("md5_hash", checksum)
d.SetId(name)
if !client.Teem {
id := uuid.New()
uniqueID := id.String()
assetInfo := f5teem.AssetInfo{
Name: "Terraform-provider-bigip",
Version: client.UserAgent,
Id: uniqueID,
}
apiKey := os.Getenv("TEEM_API_KEY")
teemDevice := f5teem.AnonymousClient(assetInfo, apiKey)
f := map[string]interface{}{
"Terraform Version": client.UserAgent,
}
tsVer := strings.Split(client.UserAgent, "/")
err = teemDevice.Report(f, "bigip_fast_template", tsVer[3])
if err != nil {
log.Printf("[ERROR]Sending Telemetry data failed:%v", err)
}
}
return resourceBigipFastRead(ctx, d, meta)
}
func resourceBigipFastRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
checksum := d.Get("md5_hash").(string)
log.Println("[INFO] Reading Fast Template Set : " + name)
template, err := client.GetTemplateSet(name)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Fast Template Set content: %+v", template)
_ = d.Set("name", template.Name)
_ = d.Set("md5_hash", checksum)
return nil
}
func resourceBigipFastUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return resourceBigipFastCreate(ctx, d, meta)
}
func resourceBigipFastDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Deleting Fast Template Set " + name)
err := client.DeleteTemplateSet(name)
if err != nil {
log.Printf("[ERROR] Unable to Delete Fast Template Set (%s) (%v) ", name, err)
return diag.FromErr(err)
}
d.SetId("")
return nil
}