github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/orchestration/handlers/cluster_handler.go (about)

     1  package handlers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/google/uuid"
    10  	"github.com/gorilla/mux"
    11  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
    12  	"github.com/kyma-project/kyma-environment-broker/internal"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/httputil"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    15  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    16  	"github.com/pkg/errors"
    17  	"github.com/sirupsen/logrus"
    18  )
    19  
    20  type clusterHandler struct {
    21  	orchestrations storage.Orchestrations
    22  	queue          *process.Queue
    23  	converter      Converter
    24  	log            logrus.FieldLogger
    25  }
    26  
    27  func NewClusterHandler(orchestrations storage.Orchestrations, q *process.Queue, log logrus.FieldLogger) *clusterHandler {
    28  	return &clusterHandler{
    29  		orchestrations: orchestrations,
    30  		queue:          q,
    31  		log:            log,
    32  		converter:      Converter{},
    33  	}
    34  }
    35  
    36  func (h *clusterHandler) AttachRoutes(router *mux.Router) {
    37  	router.HandleFunc("/upgrade/cluster", h.createOrchestration).Methods(http.MethodPost)
    38  }
    39  
    40  func (h *clusterHandler) createOrchestration(w http.ResponseWriter, r *http.Request) {
    41  	// validate request body
    42  	params := orchestration.Parameters{}
    43  	if r.Body != nil {
    44  		err := json.NewDecoder(r.Body).Decode(&params)
    45  		if err != nil {
    46  			h.log.Errorf("while decoding request body: %v", err)
    47  			httputil.WriteErrorResponse(w, http.StatusBadRequest, fmt.Errorf("while decoding request body: %w", err))
    48  			return
    49  		}
    50  	}
    51  
    52  	// validate target
    53  	err := validateTarget(params.Targets)
    54  	if err != nil {
    55  		h.log.Errorf("while validating target: %v", err)
    56  		httputil.WriteErrorResponse(w, http.StatusBadRequest, fmt.Errorf("while validating target: %w", err))
    57  		return
    58  	}
    59  
    60  	// validate deprecated parameteter `maintenanceWindow`
    61  	err = ValidateDeprecatedParameters(params)
    62  	if err != nil {
    63  		h.log.Errorf("found deprecated value: %v", err)
    64  		httputil.WriteErrorResponse(w, http.StatusBadRequest, errors.Wrapf(err, "found deprecated value"))
    65  		return
    66  	}
    67  
    68  	// validate `schedule` field
    69  	err = ValidateScheduleParameter(&params)
    70  	if err != nil {
    71  		h.log.Errorf("found deprecated value: %v", err)
    72  		httputil.WriteErrorResponse(w, http.StatusBadRequest, errors.Wrapf(err, "found deprecated value"))
    73  		return
    74  	}
    75  
    76  	now := time.Now()
    77  	o := internal.Orchestration{
    78  		OrchestrationID: uuid.New().String(),
    79  		Type:            orchestration.UpgradeClusterOrchestration,
    80  		State:           orchestration.Pending,
    81  		Description:     "queued for processing",
    82  		Parameters:      params,
    83  		CreatedAt:       now,
    84  		UpdatedAt:       now,
    85  	}
    86  
    87  	err = h.orchestrations.Insert(o)
    88  	if err != nil {
    89  		h.log.Errorf("while inserting orchestration to storage: %v", err)
    90  		httputil.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Errorf("while inserting orchestration to storage: %w", err))
    91  		return
    92  	}
    93  
    94  	h.queue.Add(o.OrchestrationID)
    95  
    96  	response := orchestration.UpgradeResponse{OrchestrationID: o.OrchestrationID}
    97  
    98  	httputil.WriteResponse(w, http.StatusAccepted, response)
    99  }