Skip to content

Commit

Permalink
Lh 68457/update ios credentials and ip when onboarded (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
weilueluo authored Jul 31, 2023
1 parent 2ac82aa commit d10f3f1
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 140 deletions.
50 changes: 25 additions & 25 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/cisco-lockhart/go-client/connector/sdc"
"github.com/cisco-lockhart/go-client/device/ios"
"github.com/cisco-lockhart/go-client/internal/device/asaconfig"

"github.com/cisco-lockhart/go-client/device/asa"
"github.com/cisco-lockhart/go-client/internal/device/asaconfig"
internalhttp "github.com/cisco-lockhart/go-client/internal/http"
)

Expand All @@ -31,50 +31,50 @@ func NewWithHttpClient(httpClient *http.Client, hostname, apiToken string) *Clie
}
}

func (c *Client) ReadAllSdcs(ctx context.Context, r sdc.ReadAllInput) (*sdc.ReadAllOutput, error) {
return sdc.ReadAll(ctx, c.client, r)
func (c *Client) ReadAllSdcs(ctx context.Context, inp sdc.ReadAllInput) (*sdc.ReadAllOutput, error) {
return sdc.ReadAll(ctx, c.client, inp)
}

func (c *Client) ReadSdcByName(ctx context.Context, r sdc.ReadByNameInput) (*sdc.ReadOutput, error) {
return sdc.ReadByName(ctx, c.client, r)
func (c *Client) ReadSdcByName(ctx context.Context, inp sdc.ReadByNameInput) (*sdc.ReadOutput, error) {
return sdc.ReadByName(ctx, c.client, inp)
}

func (c *Client) ReadAsa(ctx context.Context, r asa.ReadInput) (*asa.ReadOutput, error) {
return asa.Read(ctx, c.client, r)
func (c *Client) ReadAsa(ctx context.Context, inp asa.ReadInput) (*asa.ReadOutput, error) {
return asa.Read(ctx, c.client, inp)
}

func (c *Client) CreateAsa(ctx context.Context, r asa.CreateInput) (*asa.CreateOutput, error) {
return asa.Create(ctx, c.client, r)
func (c *Client) CreateAsa(ctx context.Context, inp asa.CreateInput) (*asa.CreateOutput, error) {
return asa.Create(ctx, c.client, inp)
}

func (c *Client) UpdateAsa(ctx context.Context, r asa.UpdateInput) (*asa.UpdateOutput, error) {
return asa.Update(ctx, c.client, r)
func (c *Client) UpdateAsa(ctx context.Context, inp asa.UpdateInput) (*asa.UpdateOutput, error) {
return asa.Update(ctx, c.client, inp)
}

func (c *Client) DeleteAsa(ctx context.Context, r asa.DeleteInput) (*asa.DeleteOutput, error) {
return asa.Delete(ctx, c.client, r)
func (c *Client) DeleteAsa(ctx context.Context, inp asa.DeleteInput) (*asa.DeleteOutput, error) {
return asa.Delete(ctx, c.client, inp)
}

func (c *Client) ReadIos(ctx context.Context, r ios.ReadInput) (*ios.ReadOutput, error) {
return ios.Read(ctx, c.client, r)
func (c *Client) ReadIos(ctx context.Context, inp ios.ReadInput) (*ios.ReadOutput, error) {
return ios.Read(ctx, c.client, inp)
}

func (c *Client) CreateIos(ctx context.Context, r ios.CreateInput) (*ios.CreateOutput, error) {
return ios.Create(ctx, c.client, r)
func (c *Client) CreateIos(ctx context.Context, inp ios.CreateInput) (*ios.CreateOutput, error) {
return ios.Create(ctx, c.client, inp)
}

func (c *Client) UpdateIos(ctx context.Context, r ios.UpdateInput) (*ios.UpdateOutput, error) {
return ios.Update(ctx, c.client, r)
func (c *Client) UpdateIos(ctx context.Context, inp ios.UpdateInput) (*ios.UpdateOutput, error) {
return ios.Update(ctx, c.client, inp)
}

func (c *Client) DeleteIos(ctx context.Context, r ios.DeleteInput) (*ios.DeleteOutput, error) {
return ios.Delete(ctx, c.client, r)
func (c *Client) DeleteIos(ctx context.Context, inp ios.DeleteInput) (*ios.DeleteOutput, error) {
return ios.Delete(ctx, c.client, inp)
}

func (c *Client) ReadAsaConfig(ctx context.Context, r asaconfig.ReadInput) (*asaconfig.ReadOutput, error) {
return asaconfig.Read(ctx, c.client, r)
func (c *Client) ReadAsaConfig(ctx context.Context, inp asaconfig.ReadInput) (*asaconfig.ReadOutput, error) {
return asaconfig.Read(ctx, c.client, inp)
}

func (c *Client) ReadSpecificAsa(ctx context.Context, r asa.ReadSpecificInput) (*asa.ReadSpecificOutput, error) {
return asa.ReadSpecific(ctx, c.client, r)
func (c *Client) ReadSpecificAsa(ctx context.Context, inp asa.ReadSpecificInput) (*asa.ReadSpecificOutput, error) {
return asa.ReadSpecific(ctx, c.client, inp)
}
16 changes: 5 additions & 11 deletions client/device/asa/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"strings"

"github.com/cisco-lockhart/go-client/connector/sdc"
"github.com/cisco-lockhart/go-client/internal/device/asaconfig"
"github.com/cisco-lockhart/go-client/internal/retry"

"github.com/cisco-lockhart/go-client/connector/sdc"

"github.com/cisco-lockhart/go-client/device"
"github.com/cisco-lockhart/go-client/internal/http"
"github.com/cisco-lockhart/go-client/internal/url"
Expand All @@ -33,15 +34,6 @@ func NewUpdateInput(uid string, name string, username string, password string) *
}
}

func NewUpdateRequest(ctx context.Context, client http.Client, updateInp UpdateInput) *http.Request {

url := url.UpdateDevice(client.BaseUrl(), updateInp.Uid)

req := client.NewPut(ctx, url, updateInp)

return req
}

func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*UpdateOutput, error) {

client.Logger.Println("updating asa device")
Expand Down Expand Up @@ -107,7 +99,9 @@ func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*Up
}
}

req := NewUpdateRequest(ctx, client, updateInp)
url := url.UpdateDevice(client.BaseUrl(), updateInp.Uid)

req := client.NewPut(ctx, url, updateInp)

var outp UpdateOutput
if err := req.Send(&outp); err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/device/ios/iosconfig/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iosconfig
import (
"context"
"encoding/json"

"github.com/cisco-lockhart/go-client/connector/sdc"

"github.com/cisco-lockhart/go-client/internal/crypto/rsa"
Expand Down
13 changes: 3 additions & 10 deletions client/device/ios/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@ func NewUpdateInput(uid string, name string) *UpdateInput {
}
}

func NewUpdateRequest(ctx context.Context, client http.Client, updateInp UpdateInput) *http.Request {

url := url.UpdateDevice(client.BaseUrl(), updateInp.Uid)

req := client.NewPut(ctx, url, updateInp)

return req
}

func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*UpdateOutput, error) {

client.Logger.Println("updating ios device")

req := NewUpdateRequest(ctx, client, updateInp)
url := url.UpdateDevice(client.BaseUrl(), updateInp.Uid)

req := client.NewPut(ctx, url, updateInp)

var outp UpdateOutput
if err := req.Send(&outp); err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/internal/device/asaconfig/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func encrypt(req *UpdateInput) error {

func makeCredentials(updateInp UpdateInput) ([]byte, error) {
if updateInp.PublicKey != nil {

if err := encrypt(&updateInp); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion provider/examples/resources/ios/ios_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ resource "cdo_ios_device" "my_ios" {
ipv4 = "<FILL_ME>"
username = "<FILL_ME>"
password = "<FILL_ME>"
}
}
4 changes: 0 additions & 4 deletions provider/internal/device/ios/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ func (d *IosDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
configData.Host = types.StringValue(readOutp.Host)
configData.IgnoreCertifcate = types.BoolValue(readOutp.IgnoreCertifcate)

// Fix: where to find them? We need them for import statement
// stateData.Username = types.StringNull()
// stateData.Password = types.StringNull()

tflog.Trace(ctx, "done read IOS device data source")

// Save data into Terraform state
Expand Down
98 changes: 98 additions & 0 deletions provider/internal/device/ios/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package ios

import (
"context"
"fmt"
"strconv"

"github.com/cisco-lockhart/go-client/connector/sdc"
"github.com/cisco-lockhart/go-client/device/ios"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func Read(ctx context.Context, resource *IosDeviceResource, stateData *IosDeviceResourceModel) error {

readInp := ios.ReadInput{
Uid: stateData.ID.ValueString(),
}

readOutp, err := resource.client.ReadIos(ctx, readInp)
if err != nil {
return err
}

port, err := strconv.ParseInt(readOutp.Port, 10, 16)
if err != nil {
return err
}

stateData.Port = types.Int64Value(port)
stateData.ID = types.StringValue(readOutp.Uid)
stateData.SdcType = types.StringValue(readOutp.LarType)
stateData.Name = types.StringValue(readOutp.Name)
stateData.Ipv4 = types.StringValue(readOutp.Ipv4)
stateData.Host = types.StringValue(readOutp.Host)
stateData.IgnoreCertifcate = types.BoolValue(readOutp.IgnoreCertifcate)

return nil
}

func Create(ctx context.Context, resource *IosDeviceResource, planData *IosDeviceResourceModel) error {

readSdcByNameInp := sdc.NewReadByNameInput(
planData.SdcName.ValueString(),
)

readSdcOutp, err := resource.client.ReadSdcByName(ctx, *readSdcByNameInp)
if err != nil {
return err
}

createInp := ios.NewCreateRequestInput(
planData.Name.ValueString(),
readSdcOutp.Uid,
planData.SdcType.ValueString(),
planData.Ipv4.ValueString(),
planData.Username.ValueString(),
planData.Password.ValueString(),
planData.IgnoreCertifcate.ValueBool(),
)

createOutp, err := resource.client.CreateIos(ctx, *createInp)
if err != nil {
return err
}

planData.ID = types.StringValue(createOutp.Uid)
planData.SdcType = types.StringValue(createOutp.LarType)
planData.SdcName = types.StringValue(planData.SdcName.ValueString())
planData.Name = types.StringValue(createOutp.Name)
planData.Host = types.StringValue(createOutp.Host)

port, err := strconv.ParseInt(createOutp.Port, 10, 16)
if err != nil {
return fmt.Errorf("failed to parse IOS port, cause=%w", err)
}
planData.Port = types.Int64Value(port)

return nil
}

func Update(ctx context.Context, resource *IosDeviceResource, planData *IosDeviceResourceModel, stateData *IosDeviceResourceModel) error {
updateInp := *ios.NewUpdateInput(
stateData.ID.ValueString(),
planData.Name.ValueString(),
)
updateOutp, err := resource.client.UpdateIos(ctx, updateInp)
if err != nil {
return err
}
stateData.Name = types.StringValue(updateOutp.Name)
return nil
}

func Delete(ctx context.Context, resource *IosDeviceResource, stateData *IosDeviceResourceModel) error {
deleteInp := ios.NewDeleteInput(stateData.ID.ValueString())
_, err := resource.client.DeleteIos(ctx, *deleteInp)
return err
}
Loading

0 comments on commit d10f3f1

Please sign in to comment.