php – Magento 2, programmatically create configurable products

Hello I have the necessity of creating both simpe and configurable products.

This is what (following online guidelines) I have right now:

$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$salt = rand(10, 999);

//REGARDING SIMPLE PRODUCTS
$simple_product = $objectManager->create('MagentoCatalogModelProduct');
$simple_product->setSku('test-simple' . $salt);
$simple_product->setName('test name simple' . $salt);
$simple_product->setAttributeSetId(9); //my own attribute set 

$simple_product->setData('157', '9'); // my custom attribute flavor and it's assigned value

$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(14)); //it's category
$simple_product->setStockData(array(
    'use_config_manage_stock' => 0, //'Use config settings'?
    'manage_stock' => 1, //manage stock
    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
    'is_in_stock' => 1, //Stock Availability
    'qty' => 100 //qty
    )
);
$simple_product->save();

$productRepository = $objectManager->get('MagentoCatalogApiProductRepositoryInterface');
$product = $productRepository->getById($simple_product->getId());
$product->setFlavor('BBBB');  //my custom attribute is called flavor and BBBB it's is value
$product->getResource()->saveAttribute($product, 'flavor');

$simple_product_id = $simple_product->getId(); //get ID
echo "simple product id: ".$simple_product_id."n";

This works just perfect, now I’d like to create a configurable product and assign this product to it:

//REGARDING CONFIGURABLE PRODUCTS
$configurable_product = $objectManager->create('MagentoCatalogModelProduct');
$configurable_product->setSku('test-configurable' . ++$salt);
$configurable_product->setName('test name configurable' . ++$salt);
$configurable_product->setAttributeSetId(9);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(14));
$configurable_product->setStockData(array(
    'use_config_manage_stock' => 0, 
    'manage_stock' => 1, 
    'is_in_stock' => 1, 
    'qty' => 999
    )
);

$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(157),$configurable_product); //attribute ID of attribute flavor

$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);

$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);

$configurableProductsData = array();
$configurableProductsData($simple_product_id) = array( //our simple product ID
    '0' => array(
        'label' => 'gusto', //attribute label
        'attribute_id' => '157', //attribute ID of attribute 'flavor' in my store
        'value_index' => '9', //value of 'BBBB' index of the attribute 
        'is_percent'    => 0,
        'pricing_value' => '10',
    )
);


$configurable_product->setConfigurableProductsData($configurableProductsData);

$configurable_product->save();

echo "configurable product id: ".$configurable_product->getId()."n";

I successifully create both products, but the simple is not associated with the configurable one.

Of course I do something wrong…. can you please guide me in the right direction?