github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/storage/storage.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package storage // import "k8s.io/helm/pkg/storage"
    18  
    19  import (
    20  	"log"
    21  
    22  	rspb "k8s.io/helm/pkg/proto/hapi/release"
    23  	"k8s.io/helm/pkg/storage/driver"
    24  )
    25  
    26  // Storage represents a storage engine for a Release.
    27  type Storage struct {
    28  	driver.Driver
    29  }
    30  
    31  // Get retrieves the release from storage. An error is returned
    32  // if the storage driver failed to fetch the release, or the
    33  // release identified by key does not exist.
    34  func (s *Storage) Get(key string) (*rspb.Release, error) {
    35  	log.Printf("Getting release %q from storage\n", key)
    36  	return s.Driver.Get(key)
    37  }
    38  
    39  // Create creates a new storage entry holding the release. An
    40  // error is returned if the storage driver failed to store the
    41  // release, or a release with identical an key already exists.
    42  func (s *Storage) Create(rls *rspb.Release) error {
    43  	log.Printf("Create release %q in storage\n", rls.Name)
    44  	return s.Driver.Create(rls)
    45  }
    46  
    47  // Update update the release in storage. An error is returned if the
    48  // storage backend fails to update the release or if the release
    49  // does not exist.
    50  func (s *Storage) Update(rls *rspb.Release) error {
    51  	log.Printf("Updating %q in storage\n", rls.Name)
    52  	return s.Driver.Update(rls)
    53  }
    54  
    55  // Delete deletes the release from storage. An error is returned if
    56  // the storage backend fails to delete the release or if the release
    57  // does not exist.
    58  func (s *Storage) Delete(key string) (*rspb.Release, error) {
    59  	log.Printf("Deleting release %q from storage\n", key)
    60  	return s.Driver.Delete(key)
    61  }
    62  
    63  // ListReleases returns all releases from storage. An error is returned if the
    64  // storage backend fails to retrieve the releases.
    65  func (s *Storage) ListReleases() ([]*rspb.Release, error) {
    66  	log.Println("Listing all releases in storage")
    67  	return s.Driver.List(func(_ *rspb.Release) bool { return true })
    68  }
    69  
    70  // ListDeleted returns all releases with Status == DELETED. An error is returned
    71  // if the storage backend fails to retrieve the releases.
    72  func (s *Storage) ListDeleted() ([]*rspb.Release, error) {
    73  	log.Println("List deleted releases in storage")
    74  	return s.Driver.List(func(rls *rspb.Release) bool {
    75  		return StatusFilter(rspb.Status_DELETED).Check(rls)
    76  	})
    77  }
    78  
    79  // ListDeployed returns all releases with Status == DEPLOYED. An error is returned
    80  // if the storage backend fails to retrieve the releases.
    81  func (s *Storage) ListDeployed() ([]*rspb.Release, error) {
    82  	log.Println("Listing all deployed releases in storage")
    83  	return s.Driver.List(func(rls *rspb.Release) bool {
    84  		return StatusFilter(rspb.Status_DEPLOYED).Check(rls)
    85  	})
    86  }
    87  
    88  // ListFilterAll returns the set of releases satisfying satisfying the predicate
    89  // (filter0 && filter1 && ... && filterN), i.e. a Release is included in the results
    90  // if and only if all filters return true.
    91  func (s *Storage) ListFilterAll(filters ...FilterFunc) ([]*rspb.Release, error) {
    92  	log.Println("Listing all releases with filter")
    93  	return s.Driver.List(func(rls *rspb.Release) bool {
    94  		return All(filters...).Check(rls)
    95  	})
    96  }
    97  
    98  // ListFilterAny returns the set of releases satisfying satisfying the predicate
    99  // (filter0 || filter1 || ... || filterN), i.e. a Release is included in the results
   100  // if at least one of the filters returns true.
   101  func (s *Storage) ListFilterAny(filters ...FilterFunc) ([]*rspb.Release, error) {
   102  	log.Println("Listing any releases with filter")
   103  	return s.Driver.List(func(rls *rspb.Release) bool {
   104  		return Any(filters...).Check(rls)
   105  	})
   106  }
   107  
   108  // Init initializes a new storage backend with the driver d.
   109  // If d is nil, the default in-memory driver is used.
   110  func Init(d driver.Driver) *Storage {
   111  	// default driver is in memory
   112  	if d == nil {
   113  		d = driver.NewMemory()
   114  	}
   115  	return &Storage{Driver: d}
   116  }