From 3993fe144682811e1e3c3fa7044e4a492220176a Mon Sep 17 00:00:00 2001 From: CY Khoo Date: Tue, 16 Apr 2024 12:26:27 +0000 Subject: [PATCH] Include service template to new service generation --- utils/services-template/Service.py | 77 +++++++++++++++++++ .../drivers/ServiceDriver.py | 33 ++++++++ utils/services-template/service.reporter.json | 16 ++++ 3 files changed, 126 insertions(+) create mode 100644 utils/services-template/Service.py create mode 100644 utils/services-template/drivers/ServiceDriver.py create mode 100644 utils/services-template/service.reporter.json diff --git a/utils/services-template/Service.py b/utils/services-template/Service.py new file mode 100644 index 0000000..b607da2 --- /dev/null +++ b/utils/services-template/Service.py @@ -0,0 +1,77 @@ +import boto3 +import botocore +import requests + +from utils.Config import Config +from services.Service import Service + +###### TO DO ##### +## Import required service module below +## Example +## from services.ec2.drivers.Ec2Instance import Ec2Instance + + +###### TO DO ##### +## Replace ServiceName with +## getResources and advise method is default method that must have +## Feel free to develop method to support your checks +class ServiceName(Service): + def __init__(self, region): + super().__init__(region) + ssBoto = self.ssBoto + + ###### TO DO ##### + ## Initiate clients required for the check + ## Example + ## self.rdsClient = ssBoto.client('rds', config=self.bConfig) + + return + + ## method to get resources for the services + ## return the array of the resources + def getResources(self): + arr = {} + + filters = [] + if self.tags: + filters = self.tags + + ###### TO DO ##### + ## list the resources + ## make sure filter by tagging is supported + ## make sure pagination is supoprted + ## Example + # results = self.ec2Client.describe_instances( + # Filters = filters + # ) + + + # arr = results.get('Reservations') + # while results.get('NextToken') is not None: + # results = self.ec2Client.describe_instances( + # Filters = filters, + # NextToken = results.get('NextToken') + # ) + # arr = arr + results.get('Reservations') + + return arr + + + def advise(self): + objs = {} + + ###### TO DO ##### + ## call getResources method + ## loop through the resources and run the checks in drivers + ## Example + # instances = self.getResources() + # for instance in instances: + # instanceData = instance['Instances'][0] + # print('... (EC2) inspecting ' + instanceData['InstanceId']) + # obj = Ec2Instance(instanceData,self.ec2Client, self.cwClient) + # obj.run(self.__class__) + + # objs[f"EC2::{instanceData['InstanceId']}"] = obj.getInfo() + #. del obj + + return objs \ No newline at end of file diff --git a/utils/services-template/drivers/ServiceDriver.py b/utils/services-template/drivers/ServiceDriver.py new file mode 100644 index 0000000..58edce4 --- /dev/null +++ b/utils/services-template/drivers/ServiceDriver.py @@ -0,0 +1,33 @@ +import boto3 +import botocore +import constants as _C + +from services.Evaluator import Evaluator + +###### TO DO ##### +## Import modules that needed for this driver +## Example +## from services.ec2.drivers.Ec2SecGroup import Ec2SecGroup + +###### TO DO ##### +## Replace ServiceDriver with + +class ServiceDriver(Evaluator): + + ###### TO DO ##### + ## Replace resource variable to meaningful name + ## Modify based on your need + def __init__(self, resource): + super().__init__() + self.init() + return + + ###### TO DO ##### + ## Change the method name to meaningful name + ## Check methods name must follow _check[Description] + def _checkDescription(self): + ###### TO DO ##### + ## Develop the checks logic here + ## If the resources failed the rules, flag the resource as example below + self.results['Rule Name'] = [-1, "Info for customer to identify the resource"] + return \ No newline at end of file diff --git a/utils/services-template/service.reporter.json b/utils/services-template/service.reporter.json new file mode 100644 index 0000000..e74487d --- /dev/null +++ b/utils/services-template/service.reporter.json @@ -0,0 +1,16 @@ +{ + "": { + "category": '', //REQUIRED, valid value (combination): O,S,R,P,C + "^description": '', //REQUIRED + "shortDesc": '', //OPTIONAL + "criticality": '', //REQUIRED, valid value (single): I, L, M, H + "downtime": '', //OPTIONAL + "slowness": '', //OPTIONAL + "additionalCost": '', //OPTIONAL + "needFullTest": '', //OPTIONAL + "ref": [ //OPTIONAL + "[]", + "[]", + ], + } +} \ No newline at end of file