| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022-2026 The Project Contributors. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
# |
| 17 |
# For a detailed list of copyright holders and contribution history, |
| 18 |
# please refer to the CONTRIBUTORS.md file in the project root. |
| 19 |
|
| 20 |
from __future__ import (absolute_import, division, print_function) |
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
DOCUMENTATION = r''' |
| 24 |
--- |
| 25 |
module: sap_company |
| 26 |
|
| 27 |
short_description: This module will manage a company entities in a SAP S4HANA environment |
| 28 |
|
| 29 |
version_added: "1.0.0" |
| 30 |
|
| 31 |
description: |
| 32 |
- The M(community.sap_libs.sap_user) module depends on C(pyrfc) Python library (version 2.4.0 and upwards). |
| 33 |
Depending on distribution you are using, you may need to install additional packages to |
| 34 |
have these available. |
| 35 |
- This module will use the company BAPIs C(BAPI_COMPANY_CLONE) and C(BAPI_COMPANY_DELETE) to manage company entities. |
| 36 |
|
| 37 |
options: |
| 38 |
state: |
| 39 |
description: |
| 40 |
- The decision what to do with the company. |
| 41 |
default: 'present' |
| 42 |
choices: |
| 43 |
- 'present' |
| 44 |
- 'absent' |
| 45 |
required: false |
| 46 |
type: str |
| 47 |
conn_username: |
| 48 |
description: The required username for the SAP system. |
| 49 |
required: true |
| 50 |
type: str |
| 51 |
conn_password: |
| 52 |
description: The required password for the SAP system. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
host: |
| 56 |
description: The required host for the SAP system. Can be either an FQDN or IP Address. |
| 57 |
required: true |
| 58 |
type: str |
| 59 |
sysnr: |
| 60 |
description: |
| 61 |
- The system number of the SAP system. |
| 62 |
- You must quote the value to ensure retaining the leading zeros. |
| 63 |
required: false |
| 64 |
default: '01' |
| 65 |
type: str |
| 66 |
client: |
| 67 |
description: |
| 68 |
- The client number to connect to. |
| 69 |
- You must quote the value to ensure retaining the leading zeros. |
| 70 |
required: false |
| 71 |
default: '000' |
| 72 |
type: str |
| 73 |
company_id: |
| 74 |
description: The company id. |
| 75 |
required: true |
| 76 |
type: str |
| 77 |
name: |
| 78 |
description: The company name. |
| 79 |
required: false |
| 80 |
type: str |
| 81 |
name_2: |
| 82 |
description: Additional company name. |
| 83 |
required: false |
| 84 |
type: str |
| 85 |
country: |
| 86 |
description: The country code for the company. For example, C('DE'). |
| 87 |
required: false |
| 88 |
type: str |
| 89 |
time_zone: |
| 90 |
description: The timezone. |
| 91 |
required: false |
| 92 |
type: str |
| 93 |
city: |
| 94 |
description: The city where the company is located. |
| 95 |
required: false |
| 96 |
type: str |
| 97 |
post_code: |
| 98 |
description: The post code from the city. |
| 99 |
required: false |
| 100 |
type: str |
| 101 |
street: |
| 102 |
description: Street where the company is located. |
| 103 |
required: false |
| 104 |
type: str |
| 105 |
street_no: |
| 106 |
description: Street number. |
| 107 |
required: false |
| 108 |
type: str |
| 109 |
e_mail: |
| 110 |
description: General E-Mail address. |
| 111 |
required: false |
| 112 |
type: str |
| 113 |
|
| 114 |
requirements: |
| 115 |
- pyrfc >= 2.4.0 |
| 116 |
|
| 117 |
author: |
| 118 |
- Rainer Leber (@rainerleber) |
| 119 |
|
| 120 |
notes: |
| 121 |
- Does not support C(check_mode). |
| 122 |
''' |
| 123 |
|
| 124 |
EXAMPLES = r''' |
| 125 |
- name: Create SAP Company |
| 126 |
community.sap_libs.sap_company: |
| 127 |
conn_username: 'DDIC' |
| 128 |
conn_password: 'HECtna2021#' |
| 129 |
host: 100.0.201.20 |
| 130 |
sysnr: '01' |
| 131 |
client: '000' |
| 132 |
state: present |
| 133 |
company_id: "Comp_ID" |
| 134 |
name: "Test_comp" |
| 135 |
name_2: "LTD" |
| 136 |
country: "DE" |
| 137 |
time_zone: "UTC" |
| 138 |
city: "City" |
| 139 |
post_code: "12345" |
| 140 |
street: "test_street" |
| 141 |
street_no: "1" |
| 142 |
e_mail: "[email protected]" |
| 143 |
|
| 144 |
# pass in a message and have changed true |
| 145 |
- name: Delete SAP Company |
| 146 |
community.sap_libs.sap_company: |
| 147 |
conn_username: 'DDIC' |
| 148 |
conn_password: 'HECtna2021#' |
| 149 |
host: 100.0.201.20 |
| 150 |
sysnr: '01' |
| 151 |
client: '000' |
| 152 |
state: absent |
| 153 |
company_id: "Comp_ID" |
| 154 |
name: "Test_comp" |
| 155 |
name_2: "LTD" |
| 156 |
country: "DE" |
| 157 |
time_zone: "UTC" |
| 158 |
city: "City" |
| 159 |
post_code: "12345" |
| 160 |
street: "test_street" |
| 161 |
street_no: "1" |
| 162 |
e_mail: "[email protected]" |
| 163 |
''' |
| 164 |
|
| 165 |
RETURN = r''' |
| 166 |
# These are examples of possible return values, and in general should use other names for return values. |
| 167 |
msg: |
| 168 |
description: A small execution description. |
| 169 |
type: str |
| 170 |
returned: always |
| 171 |
sample: 'Company address COMP_ID created' |
| 172 |
out: |
| 173 |
description: A complete description of the executed tasks. If this is available. |
| 174 |
type: list |
| 175 |
elements: dict |
| 176 |
returned: always |
| 177 |
sample: '{ |
| 178 |
"RETURN": [ |
| 179 |
{ |
| 180 |
"FIELD": "", |
| 181 |
"ID": "01", |
| 182 |
"LOG_MSG_NO": "000000", |
| 183 |
"LOG_NO": "", |
| 184 |
"MESSAGE": "Company address COMP_ID created", |
| 185 |
"MESSAGE_V1": "COMP_ID", |
| 186 |
"MESSAGE_V2": "", |
| 187 |
"MESSAGE_V3": "", |
| 188 |
"MESSAGE_V4": "", |
| 189 |
"NUMBER": "078", |
| 190 |
"PARAMETER": "", |
| 191 |
"ROW": 0, |
| 192 |
"SYSTEM": "", |
| 193 |
"TYPE": "S" |
| 194 |
} |
| 195 |
] |
| 196 |
} |
| 197 |
}' |
| 198 |
''' |
| 199 |
|
| 200 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 201 |
import traceback |
| 202 |
try: |
| 203 |
from pyrfc import Connection |
| 204 |
except ImportError: |
| 205 |
HAS_PYRFC_LIBRARY = False |
| 206 |
ANOTHER_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 207 |
else: |
| 208 |
ANOTHER_LIBRARY_IMPORT_ERROR = None |
| 209 |
HAS_PYRFC_LIBRARY = True |
| 210 |
|
| 211 |
|
| 212 |
def call_rfc_method(connection, method_name, kwargs): |
| 213 |
# PyRFC call function |
| 214 |
return connection.call(method_name, **kwargs) |
| 215 |
|
| 216 |
|
| 217 |
def build_company_params(name, name_2, country, time_zone, city, post_code, street, street_no, e_mail): |
| 218 |
# Creates RFC parameters for creating organizations |
| 219 |
# define dicts in batch |
| 220 |
params = dict() |
| 221 |
# define company name |
| 222 |
params['NAME'] = name |
| 223 |
params['NAME_2'] = name_2 |
| 224 |
# define location |
| 225 |
params['COUNTRY'] = country |
| 226 |
params['TIME_ZONE'] = time_zone |
| 227 |
params['CITY'] = city |
| 228 |
params['POSTL_COD1'] = post_code |
| 229 |
params['STREET'] = street |
| 230 |
params['STREET_NO'] = street_no |
| 231 |
# define communication |
| 232 |
params['E_MAIL'] = e_mail |
| 233 |
# return dict |
| 234 |
return params |
| 235 |
|
| 236 |
|
| 237 |
def return_analysis(raw): |
| 238 |
change = False |
| 239 |
failed = False |
| 240 |
msg = raw['RETURN'][0]['MESSAGE'] |
| 241 |
for state in raw['RETURN']: |
| 242 |
if state['TYPE'] == "E": |
| 243 |
if state['NUMBER'] == '081': |
| 244 |
change = False |
| 245 |
else: |
| 246 |
failed = True |
| 247 |
if state['TYPE'] == "S": |
| 248 |
if state['NUMBER'] != '079': |
| 249 |
change = True |
| 250 |
else: |
| 251 |
msg = "No changes where made." |
| 252 |
return [{"change": change}, {"failed": failed}, {"msg": msg}] |
| 253 |
|
| 254 |
|
| 255 |
def run_module(): |
| 256 |
module = AnsibleModule( |
| 257 |
argument_spec=dict( |
| 258 |
state=dict(default='present', choices=['absent', 'present']), |
| 259 |
conn_username=dict(type='str', required=True), |
| 260 |
conn_password=dict(type='str', required=True, no_log=True), |
| 261 |
host=dict(type='str', required=True), |
| 262 |
sysnr=dict(type='str', default="01"), |
| 263 |
client=dict(type='str', default="000"), |
| 264 |
company_id=dict(type='str', required=True), |
| 265 |
name=dict(type='str', required=False), |
| 266 |
name_2=dict(type='str', required=False), |
| 267 |
country=dict(type='str', required=False), |
| 268 |
time_zone=dict(type='str', required=False), |
| 269 |
city=dict(type='str', required=False), |
| 270 |
post_code=dict(type='str', required=False), |
| 271 |
street=dict(type='str', required=False), |
| 272 |
street_no=dict(type='str', required=False), |
| 273 |
e_mail=dict(type='str', required=False), |
| 274 |
), |
| 275 |
supports_check_mode=False, |
| 276 |
) |
| 277 |
result = dict(changed=False, msg='', out={}) |
| 278 |
raw = "" |
| 279 |
|
| 280 |
params = module.params |
| 281 |
|
| 282 |
state = params['state'] |
| 283 |
conn_username = (params['conn_username']).upper() |
| 284 |
conn_password = params['conn_password'] |
| 285 |
host = params['host'] |
| 286 |
sysnr = params['sysnr'] |
| 287 |
client = params['client'] |
| 288 |
|
| 289 |
company_id = (params['company_id']).upper() |
| 290 |
name = params['name'] |
| 291 |
name_2 = params['name_2'] |
| 292 |
country = params['country'] |
| 293 |
time_zone = params['time_zone'] |
| 294 |
city = params['city'] |
| 295 |
post_code = params['post_code'] |
| 296 |
street = params['street'] |
| 297 |
street_no = params['street_no'] |
| 298 |
e_mail = params['e_mail'] |
| 299 |
|
| 300 |
if not HAS_PYRFC_LIBRARY: |
| 301 |
module.fail_json( |
| 302 |
msg=missing_required_lib('pyrfc'), |
| 303 |
exception=ANOTHER_LIBRARY_IMPORT_ERROR) |
| 304 |
|
| 305 |
# basic RFC connection with pyrfc |
| 306 |
try: |
| 307 |
conn = Connection(user=conn_username, passwd=conn_password, ashost=host, sysnr=sysnr, client=client) |
| 308 |
except Exception as err: |
| 309 |
result['error'] = str(err) |
| 310 |
result['msg'] = 'Something went wrong connecting to the SAP system.' |
| 311 |
module.fail_json(**result) |
| 312 |
|
| 313 |
# build parameter dict of dict |
| 314 |
company_params = build_company_params(name, name_2, country, time_zone, city, post_code, street, street_no, e_mail) |
| 315 |
|
| 316 |
if state == "absent": |
| 317 |
raw = call_rfc_method(conn, 'BAPI_COMPANY_DELETE', {'COMPANY': company_id}) |
| 318 |
|
| 319 |
if state == "present": |
| 320 |
raw = call_rfc_method(conn, 'BAPI_COMPANY_CLONE', |
| 321 |
{'METHOD': {'USMETHOD': 'COMPANY_CLONE'}, 'COMPANY': company_id, 'COMP_DATA': company_params}) |
| 322 |
|
| 323 |
analysed = return_analysis(raw) |
| 324 |
|
| 325 |
result['out'] = raw |
| 326 |
|
| 327 |
result['changed'] = analysed[0]['change'] |
| 328 |
result['msg'] = analysed[2]['msg'] |
| 329 |
|
| 330 |
if analysed[1]['failed']: |
| 331 |
module.fail_json(**result) |
| 332 |
|
| 333 |
module.exit_json(**result) |
| 334 |
|
| 335 |
|
| 336 |
def main(): |
| 337 |
run_module() |
| 338 |
|
| 339 |
|
| 340 |
if __name__ == '__main__': |
| 341 |
main() |