예제 #1
0
    def __init__(self, drone_file=None):
        """
        Class for converting old Drone 1.0rc6 secrets format to the latest standard

        :keyword drone_file: path to file (Mandatory)
        """
        if drone_file is None:
            raise Exception("Keyword drone_file required")

        yaml.warnings({'YAMLLoadWarning': False})

        try:
            f = open(drone_file).read()
            self.yaml_data = list((yaml.load_all(f)))
        except:
            raise Exception("Unable to read YAML from file")
예제 #2
0
def app_upload(hostname, api_key, api_secret, source, validate_certs):
    result = {"ansible_facts": {'result': []}}
    api_endpoint = 'https://{0}'.format(hostname)
    restclient = RestClient(api_endpoint,
                            api_key=api_key,
                            api_secret=api_secret,
                            verify=validate_certs)

    app_list = []
    with open(source, 'r') as f:
        for data in oyaml.load_all(f):
            app_list.append(data)

    for i, app in enumerate(app_list):
        exists = False
        if scope['parent_app_scope_name']:
            resp = restclient.get('/openapi/v1/app_scopes')
            if not resp.status_code == 200:
                return (
                    1,
                    "Error {0}: {1} during connection attempt to {2}/openapi/v1/app_scopes. \n"
                    .format(resp.status_code, resp.reason, api_endpoint))
            current_scopes = json.loads(resp.content)
            for current_scope in current_scopes:
                if current_scope['short_name'] == scope['short_name']:
                    exists = True
            if not exists:
                scope['parent_app_scope_id'] = ParentIDLookup(
                    current_scopes, scope['parent_app_scope_name'])
                scope.pop('parent_app_scope_name')
                print('Posting scope {0} to the cluster'.format(
                    scope['short_name']))
                resp = restclient.post('/openapi/v1/app_scopes',
                                       json_body=json.dumps(scope))
                if not resp.status_code == 200:
                    return (1,
                            "Error {0}: {1} creating scope {2}. \n{3}".format(
                                resp.status_code, resp.reason,
                                scope['short_name'], resp.json()))
                result['ansible_facts']['result'].append(resp.json())

    if result['ansible_facts']['result']:
        result['changed'] = True
    else:
        result['changed'] = False
    return (0, result)
예제 #3
0
def upload(hostname,api_key,api_secret,source,validate_certs,owner_scope_id):
    result = {"ansible_facts": {}}
    api_endpoint = 'https://{0}'.format(hostname)
    restclient = RestClient(api_endpoint, api_key=api_key, api_secret=api_secret, verify=validate_certs)
    filter_list = []
    with open(source, 'r') as f:
        for data in oyaml.load_all(f):
            filter_list.append(data)

    for filter in filter_list:
        filter['app_scope_id'] = owner_scope_id
        filter.pop('id')
        filter.pop('_id')
        resp = restclient.post('/openapi/v1/filters/inventories', json_body=json.dumps(filter))

        if not resp.status_code == 200:
            return (1, "Error {0}: {1} during connection attempt to {2}/openapi/v1/filters/inventories. \n{3}".format(resp.status_code,
                                                                               resp.reason,api_endpoint,resp.json()))
    result['changed'] = True
    return (0, result)
예제 #4
0
def parse_infile(file):
    """ Parse data structure from file into dictionary for component use """
    with open(file, 'r') as data_file:
        try:
            data = json.load(data_file)
            logging.debug("Data parsed from file {}: {}".format(file, data))
            return data
        except ValueError as json_error:
            pass

        data_file.seek(0)

        try:
            data = list(yaml.load_all(data_file, Loader=yaml.loader.BaseLoader))
            logging.debug("Data parsed from file {}: {}".format(file, data))
            return data
        except yaml.YAMLError as yaml_error:
            pass

    logging.error("Unable to parse in file. {} {} ".format(json_error, yaml_error))
예제 #5
0
 def load(self):
     file_data = self.load_raw_data()
     return yaml.load_all(file_data, Loader=yaml.Loader)
예제 #6
0
def test_load_all():
    gen = yaml.load_all('{x: 1, z: 3, y: 2}\n--- {}\n')
    assert isinstance(gen, GeneratorType)
    ordered_data, empty_dict = gen
    assert empty_dict == {}
    assert ordered_data == data
def get_yaml_all(filename):
    with open(filename, 'r') as input_file:
        return list(yaml.load_all(input_file))