-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add terraform state inventory source (#14840)
* Add terraform state inventory source * Update inventory source plugin test Signed-off-by: Helen Bailey <[email protected]>
- Loading branch information
Showing
13 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
awx/main/migrations/0190_alter_inventorysource_source_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Generated by Django 4.2.6 on 2024-02-15 20:51 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0189_inbound_hop_nodes'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='inventorysource', | ||
name='source', | ||
field=models.CharField( | ||
choices=[ | ||
('file', 'File, Directory or Script'), | ||
('constructed', 'Template additional groups and hostvars at runtime'), | ||
('scm', 'Sourced from a Project'), | ||
('ec2', 'Amazon EC2'), | ||
('gce', 'Google Compute Engine'), | ||
('azure_rm', 'Microsoft Azure Resource Manager'), | ||
('vmware', 'VMware vCenter'), | ||
('satellite6', 'Red Hat Satellite 6'), | ||
('openstack', 'OpenStack'), | ||
('rhv', 'Red Hat Virtualization'), | ||
('controller', 'Red Hat Ansible Automation Platform'), | ||
('insights', 'Red Hat Insights'), | ||
('terraform', 'Terraform State'), | ||
], | ||
default=None, | ||
max_length=32, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='inventoryupdate', | ||
name='source', | ||
field=models.CharField( | ||
choices=[ | ||
('file', 'File, Directory or Script'), | ||
('constructed', 'Template additional groups and hostvars at runtime'), | ||
('scm', 'Sourced from a Project'), | ||
('ec2', 'Amazon EC2'), | ||
('gce', 'Google Compute Engine'), | ||
('azure_rm', 'Microsoft Azure Resource Manager'), | ||
('vmware', 'VMware vCenter'), | ||
('satellite6', 'Red Hat Satellite 6'), | ||
('openstack', 'OpenStack'), | ||
('rhv', 'Red Hat Virtualization'), | ||
('controller', 'Red Hat Ansible Automation Platform'), | ||
('insights', 'Red Hat Insights'), | ||
('terraform', 'Terraform State'), | ||
], | ||
default=None, | ||
max_length=32, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"TF_BACKEND_CONFIG_FILE": "{{ file_reference }}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
awx/ui/src/screens/Inventory/shared/InventorySourceSubForms/TerraformSubForm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useCallback } from 'react'; | ||
import { useField, useFormikContext } from 'formik'; | ||
import { t } from '@lingui/macro'; | ||
import getDocsBaseUrl from 'util/getDocsBaseUrl'; | ||
import { useConfig } from 'contexts/Config'; | ||
import CredentialLookup from 'components/Lookup/CredentialLookup'; | ||
import { required } from 'util/validators'; | ||
import { | ||
OptionsField, | ||
VerbosityField, | ||
EnabledVarField, | ||
EnabledValueField, | ||
HostFilterField, | ||
SourceVarsField, | ||
} from './SharedFields'; | ||
import getHelpText from '../Inventory.helptext'; | ||
|
||
const TerraformSubForm = ({ autoPopulateCredential }) => { | ||
const helpText = getHelpText(); | ||
const { setFieldValue, setFieldTouched } = useFormikContext(); | ||
const [credentialField, credentialMeta, credentialHelpers] = | ||
useField('credential'); | ||
const config = useConfig(); | ||
const handleCredentialUpdate = useCallback( | ||
(value) => { | ||
setFieldValue('credential', value); | ||
setFieldTouched('credential', true, false); | ||
}, | ||
[setFieldValue, setFieldTouched] | ||
); | ||
const docsBaseUrl = getDocsBaseUrl(config); | ||
|
||
return ( | ||
<> | ||
<CredentialLookup | ||
credentialTypeNamespace="terraform" | ||
label={t`Credential`} | ||
helperTextInvalid={credentialMeta.error} | ||
isValid={!credentialMeta.touched || !credentialMeta.error} | ||
onBlur={() => credentialHelpers.setTouched()} | ||
onChange={handleCredentialUpdate} | ||
value={credentialField.value} | ||
required | ||
autoPopulate={autoPopulateCredential} | ||
validate={required(t`Select a value for this field`)} | ||
/> | ||
<VerbosityField /> | ||
<HostFilterField /> | ||
<EnabledVarField /> | ||
<EnabledValueField /> | ||
<OptionsField /> | ||
<SourceVarsField | ||
popoverContent={helpText.sourceVars(docsBaseUrl, 'terraform')} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default TerraformSubForm; |
70 changes: 70 additions & 0 deletions
70
awx/ui/src/screens/Inventory/shared/InventorySourceSubForms/TerraformSubForm.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { Formik } from 'formik'; | ||
import { CredentialsAPI } from 'api'; | ||
import { mountWithContexts } from '../../../../../testUtils/enzymeHelpers'; | ||
import TerraformSubForm from './TerraformSubForm'; | ||
|
||
jest.mock('../../../../api'); | ||
|
||
const initialValues = { | ||
credential: null, | ||
overwrite: false, | ||
overwrite_vars: false, | ||
source_path: '', | ||
source_project: null, | ||
source_script: null, | ||
source_vars: '---\n', | ||
update_cache_timeout: 0, | ||
update_on_launch: true, | ||
verbosity: 1, | ||
}; | ||
|
||
const mockSourceOptions = { | ||
actions: { | ||
POST: {}, | ||
}, | ||
}; | ||
|
||
describe('<TerraformSubForm />', () => { | ||
let wrapper; | ||
|
||
beforeEach(async () => { | ||
CredentialsAPI.read.mockResolvedValue({ | ||
data: { count: 0, results: [] }, | ||
}); | ||
await act(async () => { | ||
wrapper = mountWithContexts( | ||
<Formik initialValues={initialValues}> | ||
<TerraformSubForm sourceOptions={mockSourceOptions} /> | ||
</Formik> | ||
); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should render subform fields', () => { | ||
expect(wrapper.find('FormGroup[label="Credential"]')).toHaveLength(1); | ||
expect(wrapper.find('FormGroup[label="Verbosity"]')).toHaveLength(1); | ||
expect(wrapper.find('FormGroup[label="Update options"]')).toHaveLength(1); | ||
expect( | ||
wrapper.find('FormGroup[label="Cache timeout (seconds)"]') | ||
).toHaveLength(1); | ||
expect( | ||
wrapper.find('VariablesField[label="Source variables"]') | ||
).toHaveLength(1); | ||
}); | ||
|
||
test('should make expected api calls', () => { | ||
expect(CredentialsAPI.read).toHaveBeenCalledTimes(1); | ||
expect(CredentialsAPI.read).toHaveBeenCalledWith({ | ||
credential_type__namespace: 'terraform', | ||
order_by: 'name', | ||
page: 1, | ||
page_size: 5, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters