summaryrefslogtreecommitdiffstats
path: root/django/engagementmanager/management/commands/clean_gitlab_content.py
blob: 52993676746444bef5316b1ec8792a6c31452321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#  
# ============LICENSE_START========================================== 
# org.onap.vvp/engagementmgr
# ===================================================================
# Copyright © 2017 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the “License”);
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
""" clean_gitlab_content
Will delete content from gitlab to create initial environment:
1.Projects
2.Groups
3.Users

This command uses gitlab client rest api to remove data.
This command is part of clean_vvp_system command but can be used separately as well.

WARNING: It will delete almost everything, if you have necessary data DO NOT USE THIS COMMAND!
"""
from django.conf import settings
from django.core.management.base import BaseCommand
import requests
from engagementmanager.service.logging_service import LoggingServiceFactory

logger = LoggingServiceFactory.get_logger()


class Command(BaseCommand):
    def handle(self, *args, **options):
        logger.info("***************************************")
        logger.info(">>git-lab is about to be cleaned up!")
        logger.info("***************************************")

        entities = ['projects', 'groups', 'users', ]
        headers = dict()
        headers['Content-type'] = 'application/json'
        headers['PRIVATE-TOKEN'] = settings.GITLAB_TOKEN

        for entity in entities:
            entities_deleted = []
            gitlab_entity_url = settings.GITLAB_URL + "api/v3/%s/" % entity
            r1 = requests.get(gitlab_entity_url, headers=headers, verify=False)
            data = r1.json()

            while len(data) > 1:
                for record in data:
                    try:
                        if record['id'] not in entities_deleted \
                                and record['name'] != 'Administrator':
                            r2 = requests.delete(gitlab_entity_url +
                                                 str(record['id']),
                                                 headers=headers, verify=False)
                            logger.info("Entity '%s' with id %s Will be deleted"
                                        " in a bit (type: %s)" % (
                                            record['name'],
                                            record['id'], entity,))

                            entities_deleted.append(record['id'])
                    except Exception as e:
                        logger.error("Some problem occurred... We give it "
                                     "another shot...", e)

                r1 = requests.get(gitlab_entity_url, headers=headers,
                                  verify=False)
                data = r1.json()

            logger.info("***************************************")
            logger.info(">>>>All %s deleted from git-lab" % entity)
            logger.info("***************************************")

        logger.info("***************************************")
        logger.info(">>git-lab is now clean!")
        logger.info("***************************************")