get.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package deployment_target
  2. import (
  3. "context"
  4. "connectrpc.com/connect"
  5. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  6. "github.com/porter-dev/api-contracts/generated/go/porter/v1/porterv1connect"
  7. "github.com/porter-dev/porter/internal/telemetry"
  8. )
  9. // DeploymentTargetDetailsInput is the input to the DeploymentTargetDetails function
  10. type DeploymentTargetDetailsInput struct {
  11. ProjectID int64
  12. ClusterID int64
  13. DeploymentTargetID string
  14. CCPClient porterv1connect.ClusterControlPlaneServiceClient
  15. }
  16. // DeploymentTarget is a struct representing the unique cluster, namespace pair for a deployment target
  17. type DeploymentTarget struct {
  18. ID string `json:"id"`
  19. Name string `json:"name"`
  20. ClusterID int64 `json:"cluster_id"`
  21. Namespace string `json:"namespace"`
  22. IsPreview bool `json:"is_preview"`
  23. IsDefault bool `json:"is_default"`
  24. }
  25. // DeploymentTargetDetails gets the deployment target details from CCP
  26. func DeploymentTargetDetails(ctx context.Context, inp DeploymentTargetDetailsInput) (DeploymentTarget, error) {
  27. ctx, span := telemetry.NewSpan(ctx, "deployment-target-details")
  28. defer span.End()
  29. var deploymentTarget DeploymentTarget
  30. if inp.ClusterID == 0 {
  31. return deploymentTarget, telemetry.Error(ctx, span, nil, "cluster id is empty")
  32. }
  33. if inp.ProjectID == 0 {
  34. return deploymentTarget, telemetry.Error(ctx, span, nil, "project id is empty")
  35. }
  36. if inp.DeploymentTargetID == "" {
  37. return deploymentTarget, telemetry.Error(ctx, span, nil, "deployment target id is empty")
  38. }
  39. if inp.CCPClient == nil {
  40. return deploymentTarget, telemetry.Error(ctx, span, nil, "cluster control plane client is nil")
  41. }
  42. deploymentTargetDetailsReq := connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
  43. ProjectId: inp.ProjectID,
  44. DeploymentTargetId: inp.DeploymentTargetID,
  45. })
  46. deploymentTargetDetailsResp, err := inp.CCPClient.DeploymentTargetDetails(ctx, deploymentTargetDetailsReq)
  47. if err != nil {
  48. return deploymentTarget, telemetry.Error(ctx, span, err, "error getting deployment target details from cluster control plane client")
  49. }
  50. if deploymentTargetDetailsResp == nil || deploymentTargetDetailsResp.Msg == nil {
  51. return deploymentTarget, telemetry.Error(ctx, span, nil, "deployment target details resp is nil")
  52. }
  53. target := deploymentTargetDetailsResp.Msg.DeploymentTarget
  54. if target.ClusterId != inp.ClusterID {
  55. return deploymentTarget, telemetry.Error(ctx, span, nil, "deployment target details resp cluster id does not match cluster id")
  56. }
  57. deploymentTarget = DeploymentTarget{
  58. ID: inp.DeploymentTargetID,
  59. Name: target.Name,
  60. Namespace: target.Namespace,
  61. ClusterID: target.ClusterId,
  62. IsPreview: target.IsPreview,
  63. IsDefault: target.IsDefault,
  64. }
  65. return deploymentTarget, nil
  66. }