R7 tag/path update
[dcaegen2/deployments.git] / cm-container / scripts / update_resolver.py
1 #!/usr/bin/env python
2 #============LICENSE_START==========================================================
3 # org.onap.dcae
4 # ==================================================================================
5 # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
6 # ==================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END===========================================================
19 #
20 import sys
21 import yaml
22 from sqlalchemy.orm.attributes import flag_modified
23 from manager_rest.flask_utils import setup_flask_app
24 from manager_rest.constants import PROVIDER_CONTEXT_ID
25 from manager_rest.storage import get_storage_manager, models
26
27
28 def main(dry_run, rules_file):
29
30     with setup_flask_app().app_context():
31         sm = get_storage_manager()
32         ctx = sm.get(models.ProviderContext, PROVIDER_CONTEXT_ID)
33         print 'Resolver rules before update:'
34         print yaml.safe_dump(ctx.context['cloudify']['import_resolver']['parameters']['rules'])
35
36         if dry_run:
37             return
38
39         with open(rules_file, 'r') as rules:
40             new_rules = yaml.load(rules)
41         ctx.context['cloudify']['import_resolver']['parameters']['rules'] = new_rules
42         print '\nResolver rules to update:'
43         print yaml.safe_dump(new_rules)
44         flag_modified(ctx, 'context')
45         sm.update(ctx)
46         print '\nProvide Context Saved'
47         print '\nResolver rules after update:'
48         print yaml.safe_dump(ctx.context['cloudify']['import_resolver']['parameters']['rules'])
49
50
51 if __name__ == '__main__':
52     if len(sys.argv) < 2:
53         print 'Must provide path to yaml file containing new rules or --dry-run'
54         exit(1)
55
56     main(sys.argv[1]=='--dry-run', sys.argv[1])