// Code generated by smithy-go-codegen DO NOT EDIT.

package applicationsignals

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/applicationsignals/types"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
	"time"
)

// Retrieves the status history for a single instrumentation configuration during
// a specified time range. The response lists when the configuration was ACTIVE,
// READY, ERROR, or DISABLED.
//
// If no status or time window is provided, the operation defaults to ACTIVE
// events from the last hour.
func (c *Client) GetInstrumentationConfigurationStatus(ctx context.Context, params *GetInstrumentationConfigurationStatusInput, optFns ...func(*Options)) (*GetInstrumentationConfigurationStatusOutput, error) {
	if params == nil {
		params = &GetInstrumentationConfigurationStatusInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "GetInstrumentationConfigurationStatus", params, optFns, c.addOperationGetInstrumentationConfigurationStatusMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*GetInstrumentationConfigurationStatusOutput)
	out.ResultMetadata = metadata
	return out, nil
}

type GetInstrumentationConfigurationStatusInput struct {

	// Environment name for the instrumentation configuration.
	//
	// This member is required.
	Environment *string

	// Type of instrumentation configuration (BREAKPOINT or PROBE). Required to
	// identify the configuration to retrieve.
	//
	// This member is required.
	InstrumentationType types.InstrumentationType

	// Location identifier - either full code location or a pre-computed hash.
	//
	// This member is required.
	LocationIdentifier types.LocationIdentifier

	// Service name for the instrumentation configuration.
	//
	// This member is required.
	Service *string

	// Signal type for the instrumentation configuration.
	//
	// This member is required.
	SignalType types.DynamicInstrumentationSignalType

	// The end of the time range to retrieve status events for. StartTime and EndTime
	// must both be provided together or both be omitted. When both are omitted, the
	// time range defaults to the last hour.
	EndTime *time.Time

	// The maximum number of status events to return in one call. The default is 60.
	MaxResults *int32

	// Use the token returned by a previous call to retrieve the next page of status
	// events.
	NextToken *string

	// The start of the time range to retrieve status events for. StartTime and EndTime
	// must both be provided together or both be omitted. When both are omitted, the
	// time range defaults to the last hour.
	StartTime *time.Time

	// The single status to query for. If omitted, only ACTIVE status events are
	// returned.
	Status types.InstrumentationConfigurationStatus

	noSmithyDocumentSerde
}

type GetInstrumentationConfigurationStatusOutput struct {

	// The environment echoed from the request.
	//
	// This member is required.
	Environment *string

	// The list of status events within the requested time window, sorted with the
	// most recent first. Error events include an error cause.
	//
	// This member is required.
	Events []types.InstrumentationStatusEvent

	// The code location echoed from the request.
	//
	// This member is required.
	Location types.Location

	// The service name echoed from the request.
	//
	// This member is required.
	Service *string

	// The telemetry signal type echoed from the request.
	//
	// This member is required.
	SignalType types.DynamicInstrumentationSignalType

	// The status that was queried. If not specified in the request, this is ACTIVE .
	//
	// This member is required.
	Status types.InstrumentationConfigurationStatus

	// Pagination token to continue retrieving status events.
	NextToken *string

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (c *Client) addOperationGetInstrumentationConfigurationStatusMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpGetInstrumentationConfigurationStatus{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpGetInstrumentationConfigurationStatus{}, middleware.After)
	if err != nil {
		return err
	}

	if err = addlegacyEndpointContextSetter(stack, options); err != nil {
		return err
	}
	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack, options); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addOpGetInstrumentationConfigurationStatusValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "GetInstrumentationConfigurationStatus"), middleware.Before); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

// GetInstrumentationConfigurationStatusPaginatorOptions is the paginator options
// for GetInstrumentationConfigurationStatus
type GetInstrumentationConfigurationStatusPaginatorOptions struct {
	// The maximum number of status events to return in one call. The default is 60.
	Limit int32

	// Set to true if pagination should stop if the service returns a pagination token
	// that matches the most recent token provided to the service.
	StopOnDuplicateToken bool
}

// GetInstrumentationConfigurationStatusPaginator is a paginator for
// GetInstrumentationConfigurationStatus
type GetInstrumentationConfigurationStatusPaginator struct {
	options   GetInstrumentationConfigurationStatusPaginatorOptions
	client    GetInstrumentationConfigurationStatusAPIClient
	params    *GetInstrumentationConfigurationStatusInput
	nextToken *string
	firstPage bool
}

// NewGetInstrumentationConfigurationStatusPaginator returns a new
// GetInstrumentationConfigurationStatusPaginator
func NewGetInstrumentationConfigurationStatusPaginator(client GetInstrumentationConfigurationStatusAPIClient, params *GetInstrumentationConfigurationStatusInput, optFns ...func(*GetInstrumentationConfigurationStatusPaginatorOptions)) *GetInstrumentationConfigurationStatusPaginator {
	if params == nil {
		params = &GetInstrumentationConfigurationStatusInput{}
	}

	options := GetInstrumentationConfigurationStatusPaginatorOptions{}
	if params.MaxResults != nil {
		options.Limit = *params.MaxResults
	}

	for _, fn := range optFns {
		fn(&options)
	}

	return &GetInstrumentationConfigurationStatusPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.NextToken,
	}
}

// HasMorePages returns a boolean indicating whether more pages are available
func (p *GetInstrumentationConfigurationStatusPaginator) HasMorePages() bool {
	return p.firstPage || (p.nextToken != nil && len(*p.nextToken) != 0)
}

// NextPage retrieves the next GetInstrumentationConfigurationStatus page.
func (p *GetInstrumentationConfigurationStatusPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*GetInstrumentationConfigurationStatusOutput, error) {
	if !p.HasMorePages() {
		return nil, fmt.Errorf("no more pages available")
	}

	params := *p.params
	params.NextToken = p.nextToken

	var limit *int32
	if p.options.Limit > 0 {
		limit = &p.options.Limit
	}
	params.MaxResults = limit

	optFns = append([]func(*Options){
		addIsPaginatorUserAgent,
	}, optFns...)
	result, err := p.client.GetInstrumentationConfigurationStatus(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.nextToken = result.NextToken

	if p.options.StopOnDuplicateToken &&
		prevToken != nil &&
		p.nextToken != nil &&
		*prevToken == *p.nextToken {
		p.nextToken = nil
	}

	return result, nil
}

// GetInstrumentationConfigurationStatusAPIClient is a client that implements the
// GetInstrumentationConfigurationStatus operation.
type GetInstrumentationConfigurationStatusAPIClient interface {
	GetInstrumentationConfigurationStatus(context.Context, *GetInstrumentationConfigurationStatusInput, ...func(*Options)) (*GetInstrumentationConfigurationStatusOutput, error)
}

var _ GetInstrumentationConfigurationStatusAPIClient = (*Client)(nil)
