github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/tsa/deleter.go (about)

     1  package tsa
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"net/http/httputil"
     8  
     9  	"fmt"
    10  
    11  	"code.cloudfoundry.org/lager"
    12  	"code.cloudfoundry.org/lager/lagerctx"
    13  	"github.com/pf-qiu/concourse/v6/atc"
    14  	"github.com/tedsuo/rata"
    15  )
    16  
    17  type Deleter struct {
    18  	ATCEndpoint *rata.RequestGenerator
    19  	HTTPClient  *http.Client
    20  }
    21  
    22  func (l *Deleter) Delete(ctx context.Context, worker atc.Worker) error {
    23  	logger := lagerctx.FromContext(ctx)
    24  
    25  	logger.Info("start")
    26  	defer logger.Info("end")
    27  
    28  	request, err := l.ATCEndpoint.CreateRequest(atc.DeleteWorker, rata.Params{
    29  		"worker_name": worker.Name,
    30  	}, nil)
    31  	if err != nil {
    32  		logger.Error("failed-to-construct-request", err)
    33  		return err
    34  	}
    35  
    36  	response, err := l.HTTPClient.Do(request)
    37  	if err != nil {
    38  		logger.Error("failed-to-delete", err)
    39  		return err
    40  	}
    41  	defer response.Body.Close()
    42  
    43  	if response.StatusCode != http.StatusOK {
    44  		logger.Error("bad-response", nil, lager.Data{
    45  			"status-code": response.StatusCode,
    46  		})
    47  
    48  		b, _ := httputil.DumpResponse(response, true)
    49  		return fmt.Errorf("bad-response (%d): %s", response.StatusCode, string(b))
    50  	}
    51  
    52  	return nil
    53  }