Add manufacturer update

main
nleiva 2022-04-28 11:53:42 -04:00
parent 72cc79531a
commit 5d335e6e18
5 changed files with 46 additions and 30 deletions

View File

@ -32,7 +32,7 @@ jobs:
- -
name: Import GPG key name: Import GPG key
id: import_gpg id: import_gpg
uses: hashicorp/ghaction-import-gpg@v2.1.0 uses: crazy-max/ghaction-import-gpg@v4.4.0
env: env:
# These secrets will need to be configured for the repository: # These secrets will need to be configured for the repository:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }} GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}

View File

@ -42,5 +42,8 @@ test:
go test -i $(TEST) || exit 1 go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
gpg-key:
gpg --armor --export-secret-key $(EMAIL) -w0 | xclip -selection clipboard -i
testacc: testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m

View File

@ -50,6 +50,11 @@ provider "nautobot" {
} }
data "nautobot_manufacturers" "all" {} data "nautobot_manufacturers" "all" {}
resource "nautobot_manufacturer" "new" {
description = "Created with Terraform"
name = "Vendor I"
}
``` ```
## Developing the Provider ## Developing the Provider

View File

@ -48,6 +48,7 @@ func resourceManufacturer() *schema.Resource {
Description: "Manufacturer's display name.", Description: "Manufacturer's display name.",
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true,
}, },
"id": { "id": {
Description: "Manufacturer's UUID.", Description: "Manufacturer's UUID.",
@ -78,19 +79,19 @@ func resourceManufacturer() *schema.Resource {
Description: "Manufacturer's slug.", Description: "Manufacturer's slug.",
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true,
}, },
"url": { "url": {
Description: "Manufacturer's URL.", Description: "Manufacturer's URL.",
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true,
}, },
}, },
} }
} }
func resourceManufacturerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { func resourceManufacturerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*apiClient).Client c := meta.(*apiClient).Client
s := meta.(*apiClient).Server s := meta.(*apiClient).Server
@ -139,11 +140,9 @@ func resourceManufacturerCreate(ctx context.Context, d *schema.ResourceData, met
} }
id := gjson.Get(string(rsp.Body), "results.0.id") id := gjson.Get(string(rsp.Body), "results.0.id")
d.Set("id", id.String())
d.SetId(id.String()) d.SetId(id.String())
resourceManufacturerRead(ctx, d, meta)
return diags return resourceManufacturerRead(ctx, d, meta)
} }
tflog.Trace(ctx, "manufacturer created", map[string]interface{}{ tflog.Trace(ctx, "manufacturer created", map[string]interface{}{
@ -153,11 +152,9 @@ func resourceManufacturerCreate(ctx context.Context, d *schema.ResourceData, met
id := gjson.Get(data, "id") id := gjson.Get(data, "id")
//d.Set("id", id.String())
d.SetId(id.String()) d.SetId(id.String())
resourceManufacturerRead(ctx, d, meta)
return diags return resourceManufacturerRead(ctx, d, meta)
} }
func resourceManufacturerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { func resourceManufacturerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@ -192,12 +189,9 @@ func resourceManufacturerRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("name", item["name"].(string)) d.Set("name", item["name"].(string))
d.Set("created", item["created"].(string)) d.Set("created", item["created"].(string))
d.Set("description", item["description"].(string)) d.Set("description", item["description"].(string))
// TODO: Fix issue with display going away
d.Set("display", item["display"].(string)) d.Set("display", item["display"].(string))
d.Set("id", item["id"].(string)) d.Set("id", item["id"].(string))
// TODO: Fix issue with slug going away
d.Set("slug", item["slug"].(string)) d.Set("slug", item["slug"].(string))
// TODO: Fix issue with url going away
d.Set("url", item["url"].(string)) d.Set("url", item["url"].(string))
d.Set("last_updated", item["last_updated"].(string)) d.Set("last_updated", item["last_updated"].(string))
@ -227,28 +221,42 @@ func resourceManufacturerRead(ctx context.Context, d *schema.ResourceData, meta
} }
func resourceManufacturerUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { func resourceManufacturerUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics c := meta.(*apiClient).Client
s := meta.(*apiClient).Server
// DcimManufacturersPartialUpdateWithBodyWithResponse request with arbitrary body returning *DcimManufacturersPartialUpdateResponse name := d.Get("name").(string)
// func (c *ClientWithResponses) DcimManufacturersPartialUpdateWithBodyWithResponse(ctx context.Context, id openapi_types.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*DcimManufacturersPartialUpdateResponse, error) { id := d.Get("id").(string)
// rsp, err := c.DcimManufacturersPartialUpdateWithBody(ctx, id, contentType, body, reqEditors...)
// if err != nil {
// return nil, err
// }
// return ParseDcimManufacturersPartialUpdateResponse(rsp)
// }
// func (c *ClientWithResponses) DcimManufacturersPartialUpdateWithResponse(ctx context.Context, id openapi_types.UUID, body DcimManufacturersPartialUpdateJSONRequestBody, reqEditors ...RequestEditorFn) (*DcimManufacturersPartialUpdateResponse, error) { var m nb.PatchedManufacturer
// rsp, err := c.DcimManufacturersPartialUpdate(ctx, id, body, reqEditors...)
// if err != nil {
// return nil, err
// }
// return ParseDcimManufacturersPartialUpdateResponse(rsp)
// }
if d.HasChange("name") {
m.Name = &name
}
desc := d.Get("description").(string)
if d.HasChange("description") {
m.Description = &desc
}
return diags slug := d.Get("slug").(string)
if d.HasChange("slug") {
m.Description = &slug
}
_, err := c.DcimManufacturersPartialUpdateWithResponse(
ctx,
types.UUID(id),
nb.DcimManufacturersPartialUpdateJSONRequestBody(m))
if err != nil {
return diag.Errorf("failed to update manufacturer %s on %s: %s", name, s, err.Error())
}
tflog.Trace(ctx, "manufacturer updated", map[string]interface{}{
"name": name,
"data": []string{desc, slug},
})
return resourceManufacturerRead(ctx, d, meta)
} }
func resourceManufacturerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { func resourceManufacturerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {