#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

from kubernetes.K8sObject import K8sObject
from kubernetes.models.v1beta1.ReplicaSet import ReplicaSet


class K8sReplicaSet(K8sObject):
    """
    http://kubernetes.io/docs/api-reference/extensions/v1beta1/definitions/#_v1beta1_replicaset
    """

    def __init__(self, config=None, name=None):

        super(K8sReplicaSet, self).__init__(
            config=config,
            obj_type='ReplicaSet',
            name=name
        )

    # -------------------------------------------------------------------------------------  fetch

    def get(self):
        self.model = ReplicaSet(self.get_model())
        return self

    def list(self, pattern=None, reverse=True):
        ls = super(K8sReplicaSet, self).list()
        rsets = list(map(lambda x: ReplicaSet(x), ls))
        if pattern is not None:
            rsets = list(filter(lambda x: pattern in x.name, rsets))
        k8s = []
        for x in rsets:
            j = K8sReplicaSet(config=self.config, name=x.name)
            j.model = x
            k8s.append(j)
        k8s.sort(key=lambda x: x.creation_timestamp, reverse=reverse)
        return k8s

    # -------------------------------------------------------------------------------------  revision

    @property
    def revision(self):
        if 'deployment.kubernetes.io/revision' in self.model.metadata.annotations:
            return self.model.metadata.annotations['deployment.kubernetes.io/revision']
        return None

    @revision.setter
    def revision(self, r=None):
        raise NotImplementedError('K8sReplicaSet: revision is read-only.')

    # -------------------------------------------------------------------------------------  revision history

    @property
    def revision_history(self):
        if 'deployment.kubernetes.io/revision-history' in self.model.metadata.annotations:
            comma_string = self.model.metadata.annotations['deployment.kubernetes.io/revision-history']
            version_array = comma_string.split(",")
            return map(lambda x: int(x), version_array)
        return None

    @revision_history.setter
    def revision_history(self, r=None):
        raise NotImplementedError('K8sReplicaSet: revision_history is read-only.')

@@ -214,16 +214,53 @@
 sObject%0A
+from kubernetes.K8sPod import K8sPod%0A
 from kub
@@ -311,16 +311,16 @@
 licaSet%0A
-
 %0A%0Aclass 
@@ -744,13 +744,16 @@
 --  
-fetch
+override
 %0A%0A  
@@ -1315,16 +1315,16 @@
 everse)%0A
-
         
@@ -1335,16 +1335,291 @@
 rn k8s%0A%0A
+    def delete(self, cascade=False):%0A        super(K8sReplicaSet, self).delete(cascade)%0A        if cascade:%0A            pods = K8sPod(config=self.config, name=%22yo%22).list(pattern=self.name)%0A            for pod in pods:%0A                pod.delete(cascade)%0A        return self%0A%0A
     # --

update delete cascade