Skip to content

Commit

Permalink
chore(LH-71991): Add workflow error message (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
weilueluo authored Nov 1, 2023
1 parent 8a91f90 commit d70afe5
Show file tree
Hide file tree
Showing 24 changed files with 225 additions and 81 deletions.
1 change: 1 addition & 0 deletions client/connector/connectoronboarding/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
ctx,
UntilConnectorStatusIsActive(ctx, client, *connector.NewReadByNameInput(createInp.Name), &readOutp),
retry.NewOptionsBuilder().
Message("Waiting for connector to be ACTIVE...").
Timeout(15*time.Minute). // usually takes ~3 minutes
Retries(-1).
Delay(2*time.Second).
Expand Down
4 changes: 3 additions & 1 deletion client/connector/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
err := retry.Do(
ctx,
untilCommunicationQueueReady(ctx, client, *NewReadByUidInput(createOutp.Uid)),
retry.NewOptionsBuilder().Retries(10).
retry.NewOptionsBuilder().
Message("Waiting for connector to be created....").
Retries(10).
Logger(client.Logger).
Delay(2*time.Second).
EarlyExitOnError(true).
Expand Down
6 changes: 4 additions & 2 deletions client/device/asa/asaconfig/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package asaconfig

import (
"context"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/statemachine"

"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url"
Expand All @@ -12,8 +13,9 @@ type ReadInput struct {
}

type ReadOutput struct {
Uid string `json:"uid"`
State string `json:"state"`
Uid string `json:"uid"`
State string `json:"state"`
StateMachineDetails statemachine.Details `json:"stateMachineDetails"`
}

func NewReadInput(specificUid string) *ReadInput {
Expand Down
8 changes: 4 additions & 4 deletions client/device/asa/asaconfig/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package asaconfig

import (
"context"
"fmt"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/statemachine"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/statemachine/state"
"strings"

Expand Down Expand Up @@ -30,13 +30,13 @@ func UntilStateDone(ctx context.Context, client http.Client, specificUid string)
return true, nil
}
if strings.EqualFold(readOutp.State, state.ERROR) {
return false, fmt.Errorf("workflow ended in ERROR")
return false, statemachine.NewWorkflowErrorFromDetails(readOutp.StateMachineDetails)
}
if strings.EqualFold(readOutp.State, state.BAD_CREDENTIALS) {
return false, fmt.Errorf("bad credentials")
return false, statemachine.NewWorkflowErrorf("Bad Credentials")
}
if strings.EqualFold(readOutp.State, state.PRE_WAIT_FOR_USER_TO_UPDATE_CREDS) {
return false, fmt.Errorf("bad credentials")
return false, statemachine.NewWorkflowErrorf("Bad Credentials")
}
return false, nil
}
Expand Down
27 changes: 25 additions & 2 deletions client/device/asa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/featureflag"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/user"
"strings"
"time"
)

type CreateInput struct {
Expand Down Expand Up @@ -129,7 +130,18 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
client.Logger.Println("waiting for asa config state done")

// poll until asa config state done
err = retry.Do(ctx, asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid), *retry.NewOptionsWithLogger(client.Logger))
err = retry.Do(
ctx,
asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid),
retry.NewOptionsBuilder().
Message("Waiting for ASA to be created...").
Retries(-1).
Delay(3*time.Second).
EarlyExitOnError(true).
Logger(client.Logger).
Timeout(3*time.Minute).
Build(),
)

// error during polling, but we maybe able to handle it
if err != nil {
Expand Down Expand Up @@ -206,7 +218,18 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
// poll until asa config state done
client.Logger.Println("waiting for device to reach state done")

err = retry.Do(ctx, asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid), *retry.NewOptionsWithLogger(client.Logger))
err = retry.Do(
ctx,
asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid),
retry.NewOptionsBuilder().
Message("Waiting for ASA to be onboarded to CDO...").
Retries(-1).
Delay(3*time.Second).
EarlyExitOnError(true).
Logger(client.Logger).
Timeout(3*time.Minute).
Build(),
)
if err != nil {
return nil, &CreateError{
CreatedResourceId: createdResourceId,
Expand Down
12 changes: 7 additions & 5 deletions client/device/asa/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/device/tags"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/devicetype"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/statemachine"

"github.com/CiscoDevnet/terraform-provider-cdo/go-client/device"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
Expand All @@ -24,11 +25,12 @@ type ReadOutput struct {
Host string `json:"host"`
Tags tags.Type `json:"tags"`

IgnoreCertificate bool `json:"ignoreCertificate"`
ConnectivityState int `json:"connectivityState,omitempty"`
ConnectivityError string `json:"connectivityError,omitempty"`
State string `json:"state"`
Status string `json:"status"`
IgnoreCertificate bool `json:"ignoreCertificate"`
ConnectivityState int `json:"connectivityState,omitempty"`
ConnectivityError string `json:"connectivityError,omitempty"`
State string `json:"state"`
Status string `json:"status"`
StateMachineDetails statemachine.Details `json:"stateMachineDetails"`
}

func NewReadInput(uid string) *ReadInput {
Expand Down
5 changes: 3 additions & 2 deletions client/device/asa/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asa
import (
"context"
"fmt"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/statemachine"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/statemachine/state"
"strings"

Expand All @@ -29,10 +30,10 @@ func UntilStateDoneAndConnectivityOk(ctx context.Context, client http.Client, ui
return true, nil
}
if strings.EqualFold(readOutp.State, state.ERROR) {
return false, fmt.Errorf("workflow ended in ERROR")
return false, statemachine.NewWorkflowErrorFromDetails(readOutp.StateMachineDetails)
}
if strings.EqualFold(readOutp.State, state.BAD_CREDENTIALS) {
return false, fmt.Errorf("bad credentials")
return false, statemachine.NewWorkflowErrorf("Bad Credentials")
}
return false, nil
}
Expand Down
36 changes: 33 additions & 3 deletions client/device/asa/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@ func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*Up
return nil, err
}

if err := retry.Do(ctx, asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid), retry.DefaultOpts); err != nil {
if err := retry.Do(
ctx,
asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid),
retry.NewOptionsBuilder().
Message("Waiting for ASA credentials to be updated on CDO...").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Timeout(retry.DefaultTimeout).
EarlyExitOnError(true).
Build(),
); err != nil {
return nil, err
}
}
Expand All @@ -100,7 +110,17 @@ func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*Up
return nil, err
}

if err := retry.Do(ctx, asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid), retry.DefaultOpts); err != nil {
if err := retry.Do(
ctx,
asaconfig.UntilStateDone(ctx, client, asaReadSpecOutp.SpecificUid),
retry.NewOptionsBuilder().
Message("Waiting for ASA location to be updated on CDO...").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Timeout(retry.DefaultTimeout).
EarlyExitOnError(true).
Build(),
); err != nil {
return nil, err
}
}
Expand All @@ -115,7 +135,17 @@ func Update(ctx context.Context, client http.Client, updateInp UpdateInput) (*Up
return nil, err
}

if err := retry.Do(ctx, UntilStateDoneAndConnectivityOk(ctx, client, outp.Uid), retry.DefaultOpts); err != nil {
if err := retry.Do(
ctx,
UntilStateDoneAndConnectivityOk(ctx, client, outp.Uid),
retry.NewOptionsBuilder().
Message("Waiting for ASA to reach connectivity state ONLINE").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Timeout(retry.DefaultTimeout).
EarlyExitOnError(true).
Build(),
); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions client/device/cloudfmc/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
ctx,
untilApplicationActive(ctx, client),
retry.NewOptionsBuilder().
Message("Waiting for cdFMC to be created...").
Retries(-1).
Timeout(30*time.Minute). // usually takes about 15-20 minutes
Delay(3*time.Second).
Expand Down
1 change: 1 addition & 0 deletions client/device/cloudfmc/fmcconfig/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func UntilCreateDeviceRecordSuccess(ctx context.Context, client http.Client, cre
ctx,
UntilTaskStatusSuccess(ctx, client, readInp),
retry.NewOptionsBuilder().
Message("Waiting for FMC device record to be created on CDO...").
Retries(-1).
Logger(client.Logger).
Timeout(30*time.Minute). // usually takes ~5 minutes
Expand Down
2 changes: 2 additions & 0 deletions client/device/cloudftd/cloudftdonboarding/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
ctx,
fmcconfig.UntilCreateDeviceRecordSuccess(ctx, client, createDeviceInp, &createOutp),
retry.NewOptionsBuilder().
Message("Waiting for FTD device record to be created on cdFMC...").
Retries(-1).
Delay(3*time.Second).
Timeout(1*time.Hour). // it can take 15-20 minutes for FTD to come up + 10 minutes to create device record
Expand Down Expand Up @@ -166,6 +167,7 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
Build(),
),
retry.NewOptionsBuilder().
Message("Waiting for FTD to be onboarded to CDO...").
Retries(-1).
Delay(1*time.Second).
Timeout(20*time.Minute). // usually done in less than 5 minutes because we already registered in FTDc
Expand Down
13 changes: 12 additions & 1 deletion client/device/cloudftd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,18 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr

// 8. wait for generate command available
var metadata Metadata
err = retry.Do(ctx, UntilGeneratedCommandAvailable(ctx, client, createOup.Uid, &metadata), *retry.NewOptionsWithLoggerAndRetries(client.Logger, 3))
err = retry.Do(
ctx,
UntilGeneratedCommandAvailable(ctx, client, createOup.Uid, &metadata),
retry.NewOptionsBuilder().
Message("Waiting for FTD record to be created in CDO...").
Retries(3).
Timeout(retry.DefaultTimeout).
Delay(retry.DefaultDelay).
Logger(client.Logger).
EarlyExitOnError(true).
Build(),
)
if err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion client/device/cloudftd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ func Delete(ctx context.Context, client http.Client, deleteInp DeleteInput) (*De
}

// 4. wait until the delete cloud FTD state machine has started
err = retry.Do(ctx, statemachine.UntilStarted(ctx, client, fmcReadSpecificRes.SpecificUid, "fmceDeleteFtdcStateMachine"), retry.DefaultOpts)
err = retry.Do(
ctx,
statemachine.UntilStarted(ctx, client, fmcReadSpecificRes.SpecificUid, "fmceDeleteFtdcStateMachine"),
retry.NewOptionsBuilder().
Message("Waiting for FTD deletion to begin...").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Logger(client.Logger).
EarlyExitOnError(true).
Timeout(retry.DefaultTimeout).
Build(),
)
if err != nil {
return nil, err
}
Expand Down
26 changes: 24 additions & 2 deletions client/device/ios/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,18 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
publicKey = &connectorReadRes.PublicKey
}

err = retry.Do(ctx, iosconfig.UntilState(ctx, client, deviceCreateOutp.Uid, state.PRE_READ_METADATA), *retry.NewOptionsWithLogger(client.Logger))
err = retry.Do(
ctx,
iosconfig.UntilState(ctx, client, deviceCreateOutp.Uid, state.PRE_READ_METADATA),
retry.NewOptionsBuilder().
Message("Waiting for IOS device to be onboarded to CDO...").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Logger(client.Logger).
EarlyExitOnError(true).
Timeout(retry.DefaultTimeout).
Build(),
)
if err != nil {
return nil, &CreateError{
Err: err,
Expand Down Expand Up @@ -149,7 +160,18 @@ func Create(ctx context.Context, client http.Client, createInp CreateInput) (*Cr
// poll until ios config state done
client.Logger.Println("waiting for device to reach state done")

err = retry.Do(ctx, iosconfig.UntilState(ctx, client, deviceCreateOutp.Uid, state.DONE), *retry.NewOptionsWithLogger(client.Logger))
err = retry.Do(
ctx,
iosconfig.UntilState(ctx, client, deviceCreateOutp.Uid, state.DONE),
retry.NewOptionsBuilder().
Message("Waiting for IOS device to be onboarded to CDO...").
Retries(retry.DefaultRetries).
Delay(retry.DefaultDelay).
Logger(client.Logger).
EarlyExitOnError(true).
Timeout(retry.DefaultTimeout).
Build(),
)
if err != nil {
return nil, &CreateError{
Err: err,
Expand Down
6 changes: 4 additions & 2 deletions client/device/ios/iosconfig/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iosconfig

import (
"context"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/model/statemachine"

"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url"
Expand All @@ -12,8 +13,9 @@ type ReadInput struct {
}

type ReadOutput struct {
Uid string `json:"uid"`
State string `json:"state"`
Uid string `json:"uid"`
State string `json:"state"`
StateMachineDetails statemachine.Details `json:"stateMachineDetails"`
}

func NewReadInput(specificUid string) *ReadInput {
Expand Down
6 changes: 3 additions & 3 deletions client/device/ios/iosconfig/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package iosconfig

import (
"context"
"fmt"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/statemachine"
"strings"

"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http"
Expand Down Expand Up @@ -30,10 +30,10 @@ func UntilState(ctx context.Context, client http.Client, specificUid string, exp
return true, nil
}
if strings.EqualFold(readOutp.State, state.ERROR) {
return false, fmt.Errorf("workflow ended in ERROR") // TODO: add workflow error message
return false, statemachine.NewWorkflowErrorFromDetails(readOutp.StateMachineDetails)
}
if strings.EqualFold(readOutp.State, state.BAD_CREDENTIALS) {
return false, fmt.Errorf("bad credentials")
return false, statemachine.NewWorkflowErrorf("Bad Credentials")
}
return false, nil
}
Expand Down
Loading

0 comments on commit d70afe5

Please sign in to comment.