Repository navigation
Expand file tree
/
Copy pathops_item.py
More file actions
157 lines (136 loc) · 4.97 KB
/
Copy pathops_item.py
File metadata and controls
157 lines (136 loc) · 4.97 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.ssm.OpsItemWrapper.class]
# snippet-start:[python.example_code.ssm.OpsItemWrapper.decl]
class OpsItemWrapper:
"""Encapsulates AWS Systems Manager OpsItem actions."""
def __init__(self, ssm_client):
"""
:param ssm_client: A Boto3 Systems Manager client.
"""
self.ssm_client = ssm_client
self.id = None
@classmethod
def from_client(cls):
"""
:return: A OpsItemWrapper instance.
"""
ssm_client = boto3.client("ssm")
return cls(ssm_client)
# snippet-end:[python.example_code.ssm.OpsItemWrapper.decl]
# snippet-start:[python.example_code.ssm.CreateOpsItem]
def create(self, title, source, category, severity, description):
"""
Create an OpsItem
:param title: The OpsItem title.
:param source: The OpsItem source.
:param category: The OpsItem category.
:param severity: The OpsItem severity.
:param description: The OpsItem description.
"""
try:
response = self.ssm_client.create_ops_item(
Title=title,
Source=source,
Category=category,
Severity=severity,
Description=description,
)
self.id = response["OpsItemId"]
except self.ssm_client.exceptions.OpsItemLimitExceededException as err:
logger.error(
"Couldn't create ops item because you have exceeded your open OpsItem limit. "
"Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
except ClientError as err:
logger.error(
"Couldn't create ops item %s. Here's why: %s: %s",
title,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ssm.CreateOpsItem]
# snippet-start:[python.example_code.ssm.DeleteOpsItem]
def delete(self):
"""
Delete the OpsItem.
"""
if self.id is None:
return
try:
self.ssm_client.delete_ops_item(OpsItemId=self.id)
print(f"Deleted ops item with id {self.id}")
self.id = None
except ClientError as err:
logger.error(
"Couldn't delete ops item %s. Here's why: %s: %s",
self.id,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ssm.DeleteOpsItem]
# snippet-start:[python.example_code.ssm.DescribeOpsItem]
def describe(self):
"""
Describe an OpsItem.
"""
try:
paginator = self.ssm_client.get_paginator("describe_ops_items")
ops_items = []
for page in paginator.paginate(
OpsItemFilters=[
{"Key": "OpsItemId", "Values": [self.id], "Operator": "Equal"}
]
):
ops_items.extend(page["OpsItemSummaries"])
for item in ops_items:
print(
f"The item title is {item['Title']} and the status is {item['Status']}"
)
return len(ops_items) > 0
except ClientError as err:
logger.error(
"Couldn't describe ops item %s. Here's why: %s: %s",
self.id,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ssm.DescribeOpsItem]
# snippet-start:[python.example_code.ssm.UpdateOpsItem]
def update(self, title=None, description=None, status=None):
"""
Update an OpsItem.
:param title: The new OpsItem title.
:param description: The new OpsItem description.
:param status: The new OpsItem status.
:return:
"""
args = dict(OpsItemId=self.id)
if title is not None:
args["Title"] = title
if description is not None:
args["Description"] = description
if status is not None:
args["Status"] = status
try:
self.ssm_client.update_ops_item(**args)
except ClientError as err:
logger.error(
"Couldn't update ops item %s. Here's why: %s: %s",
self.id,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ssm.UpdateOpsItem]
# snippet-end:[python.example_code.ssm.OpsItemWrapper.class]