github.com/argoproj/argo-cd@v1.8.7/ui/src/app/shared/services/projects-service.ts (about)

     1  import requests from './requests';
     2  
     3  import * as models from '../models';
     4  
     5  export interface CreateJWTTokenParams {
     6      project: string;
     7      role: string;
     8      expiresIn: number;
     9  }
    10  
    11  export interface DeleteJWTTokenParams {
    12      project: string;
    13      role: string;
    14      iat: number;
    15  }
    16  
    17  export interface JWTTokenResponse {
    18      token: string;
    19  }
    20  
    21  export interface ProjectSyncWindowsParams {
    22      projName: string;
    23      id: number;
    24      window: models.SyncWindow;
    25      deleteWindow: boolean;
    26  }
    27  
    28  export interface ProjectRoleParams {
    29      projName: string;
    30      roleName: string;
    31      description: string;
    32      policies: string[] | string;
    33      jwtTokens: models.JwtToken[];
    34      groups: string[];
    35      deleteRole: boolean;
    36      expiresIn: string;
    37  }
    38  
    39  function paramsToProjRole(params: ProjectRoleParams): models.ProjectRole {
    40      let newPolicies = [] as string[];
    41      if (typeof params.policies === 'string') {
    42          if (params.policies !== '') {
    43              newPolicies = params.policies.split('\n');
    44          }
    45      } else {
    46          newPolicies = params.policies;
    47      }
    48      return {
    49          name: params.roleName,
    50          description: params.description,
    51          policies: newPolicies,
    52          groups: params.groups
    53      };
    54  }
    55  
    56  export class ProjectsService {
    57      public list(...fields: string[]): Promise<models.Project[]> {
    58          return requests
    59              .get('/projects')
    60              .query({fields: fields.join(',')})
    61              .then(res => res.body as models.ProjectList)
    62              .then(list => list.items || []);
    63      }
    64  
    65      public get(name: string): Promise<models.Project> {
    66          return requests.get(`/projects/${name}`).then(res => res.body as models.Project);
    67      }
    68  
    69      public getGlobalProjects(name: string): Promise<models.Project[]> {
    70          return requests.get(`/projects/${name}/globalprojects`).then(res => res.body.items as models.Project[]);
    71      }
    72  
    73      public delete(name: string): Promise<boolean> {
    74          return requests.delete(`/projects/${name}`).then(() => true);
    75      }
    76  
    77      public create(project: models.Project): Promise<models.Project> {
    78          return requests
    79              .post('/projects')
    80              .send({project})
    81              .then(res => res.body as models.Project);
    82      }
    83  
    84      public async update(project: models.Project): Promise<models.Project> {
    85          return requests
    86              .put(`/projects/${project.metadata.name}`)
    87              .send({project})
    88              .then(res => res.body as models.Project);
    89      }
    90  
    91      public getSyncWindows(name: string): Promise<models.SyncWindowsState> {
    92          return requests
    93              .get(`/projects/${name}/syncwindows`)
    94              .query({name})
    95              .then(res => res.body as models.SyncWindowsState);
    96      }
    97  
    98      public async updateWindow(params: ProjectSyncWindowsParams): Promise<models.Project> {
    99          const proj = await this.get(params.projName);
   100          const updatedSpec = proj.spec;
   101          if (proj.spec.syncWindows === undefined) {
   102              updatedSpec.syncWindows = [];
   103          }
   104          if (params.id === undefined || !(params.id in proj.spec.syncWindows)) {
   105              updatedSpec.syncWindows = updatedSpec.syncWindows.concat(params.window);
   106          } else {
   107              if (params.deleteWindow) {
   108                  updatedSpec.syncWindows.splice(params.id, 1);
   109              } else {
   110                  updatedSpec.syncWindows[params.id] = params.window;
   111              }
   112          }
   113  
   114          return requests
   115              .put(`/projects/${params.projName}`)
   116              .send({project: {...proj, spec: updatedSpec}})
   117              .then(res => res.body as models.Project);
   118      }
   119  
   120      public async updateRole(params: ProjectRoleParams): Promise<models.Project> {
   121          const proj = await this.get(params.projName);
   122          const updatedRole = paramsToProjRole(params);
   123          let roleExist = false;
   124          if (proj.spec.roles === undefined) {
   125              proj.spec.roles = [];
   126          }
   127          const updatedSpec = proj.spec;
   128  
   129          for (let i = 0; i < proj.spec.roles.length; i++) {
   130              if (proj.spec.roles[i].name === params.roleName) {
   131                  roleExist = true;
   132                  if (params.deleteRole) {
   133                      updatedSpec.roles.splice(i, 1);
   134                      break;
   135                  }
   136                  updatedSpec.roles[i] = updatedRole;
   137              }
   138          }
   139          if (!roleExist) {
   140              if (updatedSpec.roles === undefined) {
   141                  updatedSpec.roles = [];
   142              }
   143              updatedSpec.roles = updatedSpec.roles.concat(updatedRole);
   144          }
   145          return requests
   146              .put(`/projects/${params.projName}`)
   147              .send({project: {...proj, spec: updatedSpec}})
   148              .then(res => res.body as models.Project);
   149      }
   150  
   151      public async createJWTToken(params: CreateJWTTokenParams): Promise<JWTTokenResponse> {
   152          return requests
   153              .post(`/projects/${params.project}/roles/${params.role}/token`)
   154              .send(params)
   155              .then(res => res.body as JWTTokenResponse);
   156      }
   157  
   158      public async deleteJWTToken(params: DeleteJWTTokenParams): Promise<boolean> {
   159          return requests
   160              .delete(`/projects/${params.project}/roles/${params.role}/token/${params.iat}`)
   161              .send()
   162              .then(() => true);
   163      }
   164  
   165      public events(projectName: string): Promise<models.Event[]> {
   166          return requests
   167              .get(`/projects/${projectName}/events`)
   168              .send()
   169              .then(res => (res.body as models.EventList).items || []);
   170      }
   171  }