...
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""This file contains a definition for a resource class with methods for converting samples of a single DCL resource to KCC format."""
16
17import os
18
19from absl import logging
20from dataclasses import dataclass
21from typing import List
22
23import sample
24
25
26@dataclass
27class Resource:
28 # Represents a resource whose samples are being converted from DCL to
29 # KCC.
30 service: str
31 name: str
32 kcc_kind: str
33 dcl_dir: str
34 kcc_testdata_dir: str
35 kcc_samples_dir: str
36 samples: List[sample.Sample]
37
38 def load_samples(self) -> None:
39 logging.info(f'Looking for DCL samples in:\n{self.dcl_dir}/samples')
40 self.samples = [
41 sample.Sample(
42 service=self.service,
43 dcl_dir=self.dcl_dir,
44 kcc_testdata_dir=self.kcc_testdata_dir,
45 kcc_samples_dir=self.kcc_samples_dir,
46 file_name=file_name,
47 ) for file_name in os.listdir(f'{self.dcl_dir}/samples') if
48 file_name.endswith(f'{self.name}.yaml') and 'minimal' not in file_name
49 ]
50
51 def convert_samples(self) -> None:
52 for s in self.samples:
53 s.convert()
54
55 def write_samples(self) -> None:
56 is_only = len(self.samples) == 1
57 for s in self.samples:
58 s.write(is_only)
View as plain text