MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / DocumentWrapper

Class DocumentWrapper

python/example_code/ssm/document.py:15–194  ·  view source on GitHub ↗

Encapsulates AWS Systems Manager Document actions.

Source from the content-addressed store, hash-verified

13# snippet-start:[python.example_code.ssm.DocumentWrapper.class]
14# snippet-start:[python.example_code.ssm.DocumentWrapper.decl]
15class DocumentWrapper:
16 """Encapsulates AWS Systems Manager Document actions."""
17
18 def __init__(self, ssm_client):
19 """
20 :param ssm_client: A Boto3 Systems Manager client.
21 """
22 self.ssm_client = ssm_client
23 self.name = None
24
25 @classmethod
26 def from_client(cls):
27 ssm_client = boto3.client("ssm")
28 return cls(ssm_client)
29
30 # snippet-end:[python.example_code.ssm.DocumentWrapper.decl]
31
32 # snippet-start:[python.example_code.ssm.CreateDocument]
33 def create(self, content, name):
34 """
35 Creates a document.
36
37 :param content: The content of the document.
38 :param name: The name of the document.
39 """
40 try:
41 self.ssm_client.create_document(
42 Name=name, Content=content, DocumentType="Command"
43 )
44 self.name = name
45 except self.ssm_client.exceptions.DocumentAlreadyExists:
46 print(f"Document {name} already exists.")
47 self.name = name
48 except ClientError as err:
49 logger.error(
50 "Couldn't create %s. Here's why: %s: %s",
51 name,
52 err.response["Error"]["Code"],
53 err.response["Error"]["Message"],
54 )
55 raise
56
57 # snippet-end:[python.example_code.ssm.CreateDocument]
58
59 # snippet-start:[python.example_code.ssm.DeleteDocument]
60 def delete(self):
61 """
62 Deletes an AWS Systems Manager document.
63 """
64 if self.name is None:
65 return
66
67 try:
68 self.ssm_client.delete_document(Name=self.name)
69 print(f"Deleted document {self.name}.")
70 self.name = None
71 except ClientError as err:
72 logger.error(

Callers 5

test_createFunction · 0.90
test_deleteFunction · 0.90
test_describe_documentFunction · 0.90
test_send_commandFunction · 0.90

Calls

no outgoing calls

Tested by 5

test_createFunction · 0.72
test_deleteFunction · 0.72
test_describe_documentFunction · 0.72
test_send_commandFunction · 0.72