Skip to content

Commit

Permalink
fix(LH-70705): Fix FTD onboarding fails when ftd is already register (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
weilueluo authored Oct 4, 2023
1 parent 24ea16e commit 89407a8
Show file tree
Hide file tree
Showing 19 changed files with 499 additions and 150 deletions.
11 changes: 11 additions & 0 deletions client/device/cloudfmc/fmcconfig/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fmcconfig

import "github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/cloudfmc/fmcconfig"

type Item = fmcconfig.Item

var NewItem = fmcconfig.NewItem

type Link = fmcconfig.Links

var NewLinks = fmcconfig.NewLinks
39 changes: 39 additions & 0 deletions client/device/cloudfmc/fmcconfig/read_devicerecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fmcconfig

import (
"context"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/cloudfmc/fmcconfig"
)

type ReadDeviceRecordInput struct {
FmcDomainUid string
FmcHostname string
DeviceRecordUid string
}

func NewReadDeviceRecordInput(fmcDomainUid, fmcHostname, deviceRecordUid string) ReadDeviceRecordInput {
return ReadDeviceRecordInput{
FmcDomainUid: fmcDomainUid,
FmcHostname: fmcHostname,
DeviceRecordUid: deviceRecordUid,
}
}

type ReadDeviceRecordOutput = fmcconfig.DeviceRecord

func ReadDeviceRecord(ctx context.Context, client http.Client, readInp ReadDeviceRecordInput) (*ReadDeviceRecordOutput, error) {

readUrl := url.ReadFmcDeviceRecord(client.BaseUrl(), readInp.FmcDomainUid, readInp.DeviceRecordUid)

req := client.NewGet(ctx, readUrl)
req.Header.Add("Fmc-Hostname", readInp.FmcHostname)

var readOutp fmcconfig.DeviceRecord
if err := req.Send(&readOutp); err != nil {
return nil, err
}

return &readOutp, nil
}
39 changes: 39 additions & 0 deletions client/device/cloudfmc/fmcconfig/readall_devicerecords.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fmcconfig

import (
"context"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/cloudfmc/fmcconfig"
)

type ReadAllDeviceRecordsInput struct {
FmcDomainUid string
FmcHostname string
}

func NewReadAllDeviceRecordsInput(fmcDomainUid string, fmcHostname string) ReadAllDeviceRecordsInput {
return ReadAllDeviceRecordsInput{
FmcDomainUid: fmcDomainUid,
FmcHostname: fmcHostname,
}
}

type ReadAllDeviceRecordsOutput = fmcconfig.AllDeviceRecords

var NewReadAllDeviceRecordsOutputBuilder = fmcconfig.NewAllDeviceRecordsBuilder

func ReadAllDeviceRecords(ctx context.Context, client http.Client, readInp ReadAllDeviceRecordsInput) (*ReadAllDeviceRecordsOutput, error) {

readUrl := url.ReadFmcAllDeviceRecords(client.BaseUrl(), readInp.FmcDomainUid)

req := client.NewGet(ctx, readUrl)
req.Header.Add("Fmc-Hostname", readInp.FmcHostname)

var readOutp ReadAllDeviceRecordsOutput
if err := req.Send(&readOutp); err != nil {
return nil, err
}

return &readOutp, nil
}
38 changes: 34 additions & 4 deletions client/device/cloudftd/cloudftdonboarding/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudftdonboarding
import (
"context"
"fmt"
"strings"
"time"

"github.com/CiscoDevnet/terraform-provider-cdo/go-client/device/cloudfmc"
Expand Down Expand Up @@ -49,6 +50,39 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
}
fmcDomainUid := readFmcDomainRes.Items[0].Uuid

// 1.5 check device already registered
// 1.5.1 read FTD name
readFtdOutp, err := cloudftd.ReadByUid(ctx, client, cloudftd.NewReadByUidInput(createInp.FtdUid))
if err != nil {
return nil, err
}
// 1.5.2 read all device records
allDeviceRecords, err := fmcconfig.ReadAllDeviceRecords(ctx, client, fmcconfig.NewReadAllDeviceRecordsInput(fmcDomainUid, fmcRes.Host))
if err != nil {
return nil, err
}
// 1.5.3 check if FTD name is present in device records, logic: same name + both are FTDs = duplicate
client.Logger.Printf("checking if FTD already exists with id=%s and name=%s\n", createInp.FtdUid, fmcRes.Name)
for _, record := range allDeviceRecords.Items {
if record.Name != readFtdOutp.Name {
// different name, ignore
continue
}
// the allDeviceRecords only contains the name, so we need to make another call to retrieve the details of the device to check whether this is a FTD
// potentially we will be making a lot of network calls and cause this loop to run for long time if
// we have many device records with the same name, I suppose that rarely happens
deviceRecord, err := fmcconfig.ReadDeviceRecord(ctx, client, fmcconfig.NewReadDeviceRecordInput(fmcDomainUid, fmcRes.Host, record.Id))
if err != nil {
return nil, err
}
if strings.Contains(deviceRecord.Model, "Firepower Threat Defense") { // Question: is there a better way to check? Does this check cover all cases?
return nil, fmt.Errorf("FTD with id=%s and name=%s is already registered", createInp.FtdUid, fmcRes.Name)
} else {
// not a FTD, just some other device with the same name, ignore
}
}
client.Logger.Printf("FTD with id=%s and name=%s is not registered, proceeding\n", createInp.FtdUid, fmcRes.Name)

// 2. get a system token for creating FTD device record in FMC
// CDO token does not work, we will get a 405 method not allowed if we do that
client.Logger.Println("getting a system token for creating FTD device record in FMC")
Expand All @@ -69,10 +103,6 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
client.Logger.Println("creating FTD device record in FMC")

// 3.1 read ftd metadata
readFtdOutp, err := cloudftd.ReadByUid(ctx, client, cloudftd.NewReadByUidInput(createInp.FtdUid))
if err != nil {
return nil, err
}
// 3.1.5 handle license
licenseCaps, err := license.DeserializeAllFromCdo(readFtdOutp.Metadata.LicenseCaps)
if err != nil {
Expand Down
Loading

0 comments on commit 89407a8

Please sign in to comment.