...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/workflow/workflow.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/workflow

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package workflow
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/jinzhu/gorm"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/core"
    24  )
    25  
    26  type WorkflowStore struct {
    27  	db *gorm.DB
    28  }
    29  
    30  func NewStore(db *gorm.DB) core.WorkflowStore {
    31  	db.AutoMigrate(&core.WorkflowEntity{})
    32  
    33  	return &WorkflowStore{db}
    34  }
    35  
    36  func (it *WorkflowStore) List(ctx context.Context, namespace, name string, archived bool) ([]*core.WorkflowEntity, error) {
    37  	var entities []core.WorkflowEntity
    38  	query, args := constructQueryArgs(namespace, name, "")
    39  
    40  	err := it.db.Where(query, args).Where("archived = ?", archived).Find(&entities).Error
    41  	if err != nil && !gorm.IsRecordNotFoundError(err) {
    42  		return nil, err
    43  	}
    44  
    45  	var result []*core.WorkflowEntity
    46  	for _, item := range entities {
    47  		item := item
    48  		result = append(result, &item)
    49  	}
    50  	return result, nil
    51  }
    52  
    53  func (it *WorkflowStore) ListMeta(ctx context.Context, namespace, name string, archived bool) ([]*core.WorkflowMeta, error) {
    54  	entities, err := it.List(ctx, namespace, name, archived)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	var result []*core.WorkflowMeta
    59  	for _, item := range entities {
    60  		item := item
    61  		result = append(result, &item.WorkflowMeta)
    62  	}
    63  	return result, nil
    64  }
    65  
    66  func (it *WorkflowStore) FindByID(ctx context.Context, id uint) (*core.WorkflowEntity, error) {
    67  	result := new(core.WorkflowEntity)
    68  	if err := it.db.Where(
    69  		"id = ?", id).
    70  		First(result).Error; err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	return result, nil
    75  }
    76  
    77  func (it *WorkflowStore) FindByUID(ctx context.Context, uid string) (*core.WorkflowEntity, error) {
    78  	result := new(core.WorkflowEntity)
    79  	if err := it.db.Where(
    80  		"uid = ?", uid).
    81  		First(result).Error; err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return result, nil
    86  }
    87  
    88  func (it *WorkflowStore) FindMetaByUID(ctx context.Context, UID string) (*core.WorkflowMeta, error) {
    89  	entity, err := it.FindByUID(ctx, UID)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	return &entity.WorkflowMeta, nil
    94  }
    95  
    96  func (it *WorkflowStore) Save(ctx context.Context, entity *core.WorkflowEntity) error {
    97  	return it.db.Model(core.WorkflowEntity{}).Save(entity).Error
    98  }
    99  
   100  func (it *WorkflowStore) DeleteByUID(ctx context.Context, uid string) error {
   101  	return it.db.Where("uid = ?", uid).Unscoped().
   102  		Delete(core.WorkflowEntity{}).Error
   103  }
   104  
   105  func (it *WorkflowStore) DeleteByUIDs(ctx context.Context, uids []string) error {
   106  	return it.db.Where("uid IN (?)", uids).Unscoped().Delete(core.WorkflowEntity{}).Error
   107  }
   108  
   109  func (it *WorkflowStore) MarkAsArchived(ctx context.Context, namespace, name string) error {
   110  	if err := it.db.Model(core.WorkflowEntity{}).
   111  		Where("namespace = ? AND name = ? AND archived = ?", namespace, name, false).
   112  		Updates(map[string]interface{}{"archived": true}).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
   113  		return err
   114  	}
   115  	return nil
   116  }
   117  
   118  func (it *WorkflowStore) MarkAsArchivedWithUID(ctx context.Context, uid string) error {
   119  	if err := it.db.Model(core.WorkflowEntity{}).
   120  		Where("uid = ? AND archived = ?", uid, false).
   121  		Updates(map[string]interface{}{"archived": true}).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
   122  		return err
   123  	}
   124  	return nil
   125  }
   126  func constructQueryArgs(ns, name, uid string) (string, []string) {
   127  	query := ""
   128  	args := make([]string, 0)
   129  
   130  	if ns != "" {
   131  		if len(args) > 0 {
   132  			query += " AND namespace = ?"
   133  		} else {
   134  			query += "namespace = ?"
   135  		}
   136  		args = append(args, ns)
   137  	}
   138  
   139  	if name != "" {
   140  		if len(args) > 0 {
   141  			query += " AND name = ?"
   142  		} else {
   143  			query += "name = ?"
   144  		}
   145  		args = append(args, name)
   146  	}
   147  
   148  	if uid != "" {
   149  		if len(args) > 0 {
   150  			query += " AND uid = ?"
   151  		} else {
   152  			query += "uid = ?"
   153  		}
   154  		args = append(args, uid)
   155  	}
   156  
   157  	return query, args
   158  }
   159