...

Source file src/github.com/redhat-developer/odo/pkg/apiserver-impl/devstate.go

Documentation: github.com/redhat-developer/odo/pkg/apiserver-impl

     1  package apiserver_impl
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	openapi "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
     9  	"github.com/redhat-developer/odo/pkg/apiserver-impl/devstate"
    10  )
    11  
    12  func (s *DevstateApiService) DevstateContainerPost(ctx context.Context, container openapi.DevstateContainerPostRequest) (openapi.ImplResponse, error) {
    13  	newContent, err := s.devfileState.AddContainer(
    14  		container.Name,
    15  		container.Image,
    16  		container.Command,
    17  		container.Args,
    18  		container.Env,
    19  		container.MemReq,
    20  		container.MemLimit,
    21  		container.CpuReq,
    22  		container.CpuLimit,
    23  		container.VolumeMounts,
    24  		container.ConfigureSources,
    25  		container.MountSources,
    26  		container.SourceMapping,
    27  		container.Annotation,
    28  		container.Endpoints,
    29  	)
    30  	if err != nil {
    31  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    32  			Message: fmt.Sprintf("Error adding the container: %s", err),
    33  		}), nil
    34  	}
    35  	return openapi.Response(http.StatusOK, newContent), nil
    36  }
    37  
    38  func (s *DevstateApiService) DevstateContainerContainerNameDelete(ctx context.Context, containerName string) (openapi.ImplResponse, error) {
    39  	newContent, err := s.devfileState.DeleteContainer(containerName)
    40  	if err != nil {
    41  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    42  			Message: fmt.Sprintf("Error deleting the container: %s", err),
    43  		}), nil
    44  	}
    45  	return openapi.Response(http.StatusOK, newContent), nil
    46  }
    47  
    48  func (s *DevstateApiService) DevstateImagePost(ctx context.Context, image openapi.DevstateImagePostRequest) (openapi.ImplResponse, error) {
    49  	newContent, err := s.devfileState.AddImage(
    50  		image.Name,
    51  		image.ImageName,
    52  		image.Args,
    53  		image.BuildContext,
    54  		image.RootRequired,
    55  		image.Uri,
    56  		image.AutoBuild,
    57  	)
    58  	if err != nil {
    59  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    60  			Message: fmt.Sprintf("Error adding the image: %s", err),
    61  		}), nil
    62  	}
    63  	return openapi.Response(http.StatusOK, newContent), nil
    64  }
    65  
    66  func (s *DevstateApiService) DevstateImageImageNameDelete(ctx context.Context, imageName string) (openapi.ImplResponse, error) {
    67  	newContent, err := s.devfileState.DeleteImage(imageName)
    68  	if err != nil {
    69  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    70  			Message: fmt.Sprintf("Error deleting the image: %s", err),
    71  		}), nil
    72  	}
    73  	return openapi.Response(http.StatusOK, newContent), nil
    74  }
    75  
    76  func (s *DevstateApiService) DevstateResourcePost(ctx context.Context, resource openapi.DevstateResourcePostRequest) (openapi.ImplResponse, error) {
    77  	newContent, err := s.devfileState.AddResource(
    78  		resource.Name,
    79  		resource.Inlined,
    80  		resource.Uri,
    81  		resource.DeployByDefault,
    82  	)
    83  	if err != nil {
    84  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    85  			Message: fmt.Sprintf("Error adding the resource: %s", err),
    86  		}), nil
    87  	}
    88  	return openapi.Response(http.StatusOK, newContent), nil
    89  
    90  }
    91  
    92  func (s *DevstateApiService) DevstateResourceResourceNameDelete(ctx context.Context, resourceName string) (openapi.ImplResponse, error) {
    93  	newContent, err := s.devfileState.DeleteResource(resourceName)
    94  	if err != nil {
    95  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
    96  			Message: fmt.Sprintf("Error deleting the resource: %s", err),
    97  		}), nil
    98  	}
    99  	return openapi.Response(http.StatusOK, newContent), nil
   100  }
   101  
   102  func (s *DevstateApiService) DevstateVolumePost(ctx context.Context, volume openapi.DevstateVolumePostRequest) (openapi.ImplResponse, error) {
   103  	newContent, err := s.devfileState.AddVolume(
   104  		volume.Name,
   105  		volume.Ephemeral,
   106  		volume.Size,
   107  	)
   108  	if err != nil {
   109  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   110  			Message: fmt.Sprintf("Error adding the volume: %s", err),
   111  		}), nil
   112  	}
   113  	return openapi.Response(http.StatusOK, newContent), nil
   114  }
   115  
   116  func (s *DevstateApiService) DevstateVolumeVolumeNameDelete(ctx context.Context, volumeName string) (openapi.ImplResponse, error) {
   117  	newContent, err := s.devfileState.DeleteVolume(volumeName)
   118  	if err != nil {
   119  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   120  			Message: fmt.Sprintf("Error deleting the volume: %s", err),
   121  		}), nil
   122  	}
   123  	return openapi.Response(http.StatusOK, newContent), nil
   124  }
   125  
   126  func (s *DevstateApiService) DevstateApplyCommandPost(ctx context.Context, command openapi.DevstateApplyCommandPostRequest) (openapi.ImplResponse, error) {
   127  	newContent, err := s.devfileState.AddApplyCommand(
   128  		command.Name,
   129  		command.Component,
   130  	)
   131  	if err != nil {
   132  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   133  			Message: fmt.Sprintf("Error adding the Apply command: %s", err),
   134  		}), nil
   135  	}
   136  	return openapi.Response(http.StatusOK, newContent), nil
   137  }
   138  
   139  func (s *DevstateApiService) DevstateCommandCommandNameDelete(ctx context.Context, commandName string) (openapi.ImplResponse, error) {
   140  	newContent, err := s.devfileState.DeleteCommand(commandName)
   141  	if err != nil {
   142  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   143  			Message: fmt.Sprintf("Error deleting the command: %s", err),
   144  		}), nil
   145  	}
   146  	return openapi.Response(http.StatusOK, newContent), nil
   147  }
   148  
   149  func (s *DevstateApiService) DevstateCompositeCommandPost(ctx context.Context, command openapi.DevstateCompositeCommandPostRequest) (openapi.ImplResponse, error) {
   150  	newContent, err := s.devfileState.AddCompositeCommand(
   151  		command.Name,
   152  		command.Parallel,
   153  		command.Commands,
   154  	)
   155  	if err != nil {
   156  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   157  			Message: fmt.Sprintf("Error adding the Composite command: %s", err),
   158  		}), nil
   159  	}
   160  	return openapi.Response(http.StatusOK, newContent), nil
   161  
   162  }
   163  
   164  func (s *DevstateApiService) DevstateExecCommandPost(ctx context.Context, command openapi.DevstateExecCommandPostRequest) (openapi.ImplResponse, error) {
   165  	newContent, err := s.devfileState.AddExecCommand(
   166  		command.Name,
   167  		command.Component,
   168  		command.CommandLine,
   169  		command.WorkingDir,
   170  		command.HotReloadCapable,
   171  	)
   172  	if err != nil {
   173  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   174  			Message: fmt.Sprintf("Error adding the Exec command: %s", err),
   175  		}), nil
   176  	}
   177  	return openapi.Response(http.StatusOK, newContent), nil
   178  }
   179  
   180  func (s *DevstateApiService) DevstateMetadataPut(ctx context.Context, metadata openapi.MetadataRequest) (openapi.ImplResponse, error) {
   181  	newContent, err := s.devfileState.SetMetadata(
   182  		metadata.Name,
   183  		metadata.Version,
   184  		metadata.DisplayName,
   185  		metadata.Description,
   186  		metadata.Tags,
   187  		metadata.Architectures,
   188  		metadata.Icon,
   189  		metadata.GlobalMemoryLimit,
   190  		metadata.ProjectType,
   191  		metadata.Language,
   192  		metadata.Website,
   193  		metadata.Provider,
   194  		metadata.SupportUrl,
   195  	)
   196  	if err != nil {
   197  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   198  			Message: fmt.Sprintf("Error updating the metadata: %s", err),
   199  		}), nil
   200  	}
   201  	return openapi.Response(http.StatusOK, newContent), nil
   202  }
   203  
   204  func (s *DevstateApiService) DevstateChartGet(context.Context) (openapi.ImplResponse, error) {
   205  	chart, err := s.devfileState.GetFlowChart()
   206  	if err != nil {
   207  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   208  			Message: fmt.Sprintf("Error building the Devfile cycle chart: %s", err),
   209  		}), nil
   210  	}
   211  	return openapi.Response(http.StatusOK, openapi.DevstateChartGet200Response{
   212  		Chart: chart,
   213  	}), nil
   214  }
   215  
   216  func (s *DevstateApiService) DevstateCommandCommandNameMovePost(ctx context.Context, commandName string, params openapi.DevstateCommandCommandNameMovePostRequest) (openapi.ImplResponse, error) {
   217  	newContent, err := s.devfileState.MoveCommand(
   218  		params.FromGroup,
   219  		params.ToGroup,
   220  		int(params.FromIndex),
   221  		int(params.ToIndex),
   222  	)
   223  	if err != nil {
   224  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   225  			Message: fmt.Sprintf("Error moving command to group %q index %d: %s", params.ToGroup, params.ToIndex, err),
   226  		}), nil
   227  	}
   228  	return openapi.Response(http.StatusOK, newContent), nil
   229  }
   230  
   231  func (s *DevstateApiService) DevstateCommandCommandNameSetDefaultPost(ctx context.Context, commandName string, params openapi.DevstateCommandCommandNameSetDefaultPostRequest) (openapi.ImplResponse, error) {
   232  	newContent, err := s.devfileState.SetDefaultCommand(commandName, params.Group)
   233  	if err != nil {
   234  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   235  			Message: fmt.Sprintf("Error setting command %q as default for group %q: %s", commandName, params.Group, err),
   236  		}), nil
   237  	}
   238  	return openapi.Response(http.StatusOK, newContent), nil
   239  }
   240  
   241  func (s *DevstateApiService) DevstateCommandCommandNameUnsetDefaultPost(ctx context.Context, commandName string) (openapi.ImplResponse, error) {
   242  	newContent, err := s.devfileState.UnsetDefaultCommand(commandName)
   243  	if err != nil {
   244  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   245  			Message: fmt.Sprintf("Error unsetting command %q as default: %s", commandName, err),
   246  		}), nil
   247  	}
   248  	return openapi.Response(http.StatusOK, newContent), nil
   249  }
   250  
   251  func (s *DevstateApiService) DevstateEventsPut(ctx context.Context, params openapi.DevstateEventsPutRequest) (openapi.ImplResponse, error) {
   252  	newContent, err := s.devfileState.UpdateEvents(params.EventName, params.Commands)
   253  	if err != nil {
   254  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   255  			Message: fmt.Sprintf("Error updating commands for event %q: %s", params.EventName, err),
   256  		}), nil
   257  	}
   258  	return openapi.Response(http.StatusOK, newContent), nil
   259  }
   260  
   261  func (s *DevstateApiService) DevstateQuantityValidPost(ctx context.Context, params openapi.DevstateQuantityValidPostRequest) (openapi.ImplResponse, error) {
   262  	result := devstate.IsQuantityValid(params.Quantity)
   263  	if !result {
   264  		return openapi.Response(http.StatusBadRequest, openapi.GeneralError{
   265  			Message: fmt.Sprintf("Quantity %q is not valid", params.Quantity),
   266  		}), nil
   267  	}
   268  	return openapi.Response(http.StatusOK, openapi.GeneralSuccess{
   269  		Message: fmt.Sprintf("Quantity %q is valid", params.Quantity),
   270  	}), nil
   271  }
   272  
   273  func (s *DevstateApiService) DevstateDevfilePut(ctx context.Context, params openapi.DevstateDevfilePutRequest) (openapi.ImplResponse, error) {
   274  	newContent, err := s.devfileState.SetDevfileContent(params.Content)
   275  	if err != nil {
   276  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   277  			Message: fmt.Sprintf("Error setting new Devfile content: %s", err),
   278  		}), nil
   279  	}
   280  	return openapi.Response(http.StatusOK, newContent), nil
   281  }
   282  
   283  func (s *DevstateApiService) DevstateDevfileGet(context.Context) (openapi.ImplResponse, error) {
   284  	newContent, err := s.devfileState.GetContent()
   285  	if err != nil {
   286  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   287  			Message: fmt.Sprintf("Error getting new Devfile content: %s", err),
   288  		}), nil
   289  	}
   290  	return openapi.Response(http.StatusOK, newContent), nil
   291  }
   292  
   293  func (s *DevstateApiService) DevstateDevfileDelete(context.Context) (openapi.ImplResponse, error) {
   294  	newContent, err := s.devfileState.SetDevfileContent(`schemaVersion: 2.2.0`)
   295  	if err != nil {
   296  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   297  			Message: fmt.Sprintf("Error clearing Devfile content: %s", err),
   298  		}), nil
   299  	}
   300  	return openapi.Response(http.StatusOK, newContent), nil
   301  }
   302  
   303  func (s *DevstateApiService) DevstateVolumeVolumeNamePatch(ctx context.Context, name string, patch openapi.DevstateVolumeVolumeNamePatchRequest) (openapi.ImplResponse, error) {
   304  	newContent, err := s.devfileState.PatchVolume(
   305  		name,
   306  		patch.Ephemeral,
   307  		patch.Size,
   308  	)
   309  	if err != nil {
   310  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   311  			Message: fmt.Sprintf("Error updating the volume: %s", err),
   312  		}), nil
   313  	}
   314  	return openapi.Response(http.StatusOK, newContent), nil
   315  }
   316  
   317  func (s *DevstateApiService) DevstateResourceResourceNamePatch(ctx context.Context, name string, patch openapi.DevstateResourceResourceNamePatchRequest) (openapi.ImplResponse, error) {
   318  	newContent, err := s.devfileState.PatchResource(
   319  		name,
   320  		patch.Inlined,
   321  		patch.Uri,
   322  		patch.DeployByDefault,
   323  	)
   324  	if err != nil {
   325  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   326  			Message: fmt.Sprintf("Error updating the resource: %s", err),
   327  		}), nil
   328  	}
   329  	return openapi.Response(http.StatusOK, newContent), nil
   330  }
   331  
   332  func (s *DevstateApiService) DevstateImageImageNamePatch(ctx context.Context, name string, patch openapi.DevstateImageImageNamePatchRequest) (openapi.ImplResponse, error) {
   333  	newContent, err := s.devfileState.PatchImage(
   334  		name,
   335  		patch.ImageName,
   336  		patch.Args,
   337  		patch.BuildContext,
   338  		patch.RootRequired,
   339  		patch.Uri,
   340  		patch.AutoBuild,
   341  	)
   342  	if err != nil {
   343  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   344  			Message: fmt.Sprintf("Error updating the image: %s", err),
   345  		}), nil
   346  	}
   347  	return openapi.Response(http.StatusOK, newContent), nil
   348  }
   349  
   350  func (s *DevstateApiService) DevstateExecCommandCommandNamePatch(ctx context.Context, name string, patch openapi.DevstateExecCommandCommandNamePatchRequest) (openapi.ImplResponse, error) {
   351  	newContent, err := s.devfileState.PatchExecCommand(
   352  		name,
   353  		patch.Component,
   354  		patch.CommandLine,
   355  		patch.WorkingDir,
   356  		patch.HotReloadCapable,
   357  	)
   358  	if err != nil {
   359  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   360  			Message: fmt.Sprintf("Error updating the Exec Command: %s", err),
   361  		}), nil
   362  	}
   363  	return openapi.Response(http.StatusOK, newContent), nil
   364  }
   365  
   366  func (s *DevstateApiService) DevstateApplyCommandCommandNamePatch(ctx context.Context, name string, patch openapi.DevstateApplyCommandCommandNamePatchRequest) (openapi.ImplResponse, error) {
   367  	newContent, err := s.devfileState.PatchApplyCommand(
   368  		name,
   369  		patch.Component,
   370  	)
   371  	if err != nil {
   372  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   373  			Message: fmt.Sprintf("Error updating the Apply Command: %s", err),
   374  		}), nil
   375  	}
   376  	return openapi.Response(http.StatusOK, newContent), nil
   377  }
   378  
   379  func (s *DevstateApiService) DevstateCompositeCommandCommandNamePatch(ctx context.Context, name string, patch openapi.DevstateCompositeCommandCommandNamePatchRequest) (openapi.ImplResponse, error) {
   380  	newContent, err := s.devfileState.PatchCompositeCommand(
   381  		name,
   382  		patch.Parallel,
   383  		patch.Commands,
   384  	)
   385  	if err != nil {
   386  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   387  			Message: fmt.Sprintf("Error updating the Composite Command: %s", err),
   388  		}), nil
   389  	}
   390  	return openapi.Response(http.StatusOK, newContent), nil
   391  }
   392  
   393  func (s *DevstateApiService) DevstateContainerContainerNamePatch(ctx context.Context, name string, patch openapi.DevstateContainerContainerNamePatchRequest) (openapi.ImplResponse, error) {
   394  	newContent, err := s.devfileState.PatchContainer(
   395  		name,
   396  		patch.Image,
   397  		patch.Command,
   398  		patch.Args,
   399  		patch.Env,
   400  		patch.MemReq,
   401  		patch.MemLimit,
   402  		patch.CpuReq,
   403  		patch.CpuLimit,
   404  		patch.VolumeMounts,
   405  		patch.ConfigureSources,
   406  		patch.MountSources,
   407  		patch.SourceMapping,
   408  		patch.Annotation,
   409  		patch.Endpoints,
   410  	)
   411  	if err != nil {
   412  		return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
   413  			Message: fmt.Sprintf("Error updating the container: %s", err),
   414  		}), nil
   415  	}
   416  	return openapi.Response(http.StatusOK, newContent), nil
   417  }
   418  

View as plain text