-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLOClass.py
60 lines (50 loc) · 2.54 KB
/
SLOClass.py
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
# Copyright 2024 Sean Kunkler
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file 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.
import requests
from helperFunctions import *
class SLO:
def __init__(self, slo_id, SOC):
self.__slo_id = slo_id
self.__soc = SOC
self.__slo_name = self.__fetchName()
self.__tf_item_name = setTfName(self.__slo_name)
self.__initialWrite()
self.__importTerraform()
def __importTerraform(self):
if not self.__soc._getFuse():
terraformInit(self.__soc._getCWD(),
self.__soc._getAPIKey(), self.__soc._getRealm(), self.__soc._getVerbose())
terraformImport(f"signalfx_slo.{self.__tf_item_name}", self.__slo_id, self.__soc._getCWD(
), self.__soc._getAPIKey(), self.__soc._getRealm(), self.__soc._getVerbose())
self.__terraform = terraformState(f"signalfx_slo.{self.__tf_item_name}", self.__slo_id, self.__soc._getCWD(
), self.__soc._getAPIKey(), self.__soc._getRealm(), self.__soc._getVerbose())
if isinstance(self.__terraform, int):
return
self.__terraform = '\n'.join(line for line in self.__terraform.split(
'\n') if not re.search(r'\b(url|id|config_id|tags)\s*=', line))
return
def __initialWrite(self):
with open(f"{self.__soc._getCWD()}/{self.__tf_item_name}.tf", "w") as f:
f.write(
f"resource \"signalfx_slo\" \"{self.__tf_item_name}\" {{\n")
f.write(f"}}\n")
def __fetchName(self):
url = f"https://api.{self.__soc._getRealm()}.signalfx.com/v2/slo/{self.__slo_id}"
headers = {"X-SF-TOKEN": self.__soc._getAPIKey()}
response = requests.get(url, headers=headers)
response.raise_for_status()
slo_name = response.json()['name']
return slo_name
def _produceTerraform(self):
with open(f"{self.__soc._getCWD()}/{self.__tf_item_name}.tf", "w") as f:
self.__terraform = "\n".join([line for line in self.__terraform.split('\n') if '[90mnull[0m[0m' not in line])
f.write(self.__terraform)
return